I trying to use SubString() to return a portion of an existing AnsiString.
The following worked as expected with BCB3, but not with BCB5.
============================
AnsiString TempString;
int FileNameLength;
...
// Want to remove the file extension "path\filename.swr"
FileNameLength = OpenDialog->FileName.Length() - 4;
TempString = OpenDialog->FileName.SubString(0, FileNameLength);
=============================
TempString is to NULL not to "path\filename"
I've tried using an index of 1 for the first argument of SubString, but get
the same NULL return value.
Any thoughts???
Thnaks Mike
> AnsiString TempString;
> int FileNameLength;
> ...
>
> // Want to remove the file extension "path\filename.swr"
> FileNameLength = OpenDialog->FileName.Length() - 4;
>
> TempString = OpenDialog->FileName.SubString(0, FileNameLength);
> =============================
>
> TempString is to NULL not to "path\filename"
I created a new app, added an OpenDialog, a Button and a Label and wrote
the following code. It works as expected.
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString TempString;
int FileNameLength;
if (OpenDialog1->Execute())
{
FileNameLength = OpenDialog1->FileName.Length() - 4;
TempString = OpenDialog1->FileName.SubString(1, FileNameLength);
Label1->Caption = TempString;
}
}
The index of 0 is out of range. BCB3 (and Delphi 3) were quite forgiving
regarding this (they would accept negative indices), but since D5 (and
BCB5) this is not true anymore. When I changed the 1 into a 0, I got a
NULL string, like you.
--
Rudy Velthuis (TeamB) http://delphi-jedi.org
Thanks, Mike
> Appreciate you checking the SubString() method. I'll try again using
> the index of 1. The online help file for the SubString() wasn't clear as
to
> the base (either 0 or 1) for index....
AnsiStrings are 1-based, so the index argument of SubString ought to
be 1-based too (it is the index of the first character).
"Rudy Velthuis (TeamB)" <rvel...@gmx.de> wrote in message
news:95k8cn$dv...@bornews.inprise.com...
have a look at ExtractFilePath(const AnsiString FileName);
there are others too.
> CBuilder provides some routines for handling drive/path/ext
ChangeFileExt() will do the trick:
TempString = ChangeFileExt(OpenDialog1->FileName, "");