Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

AnsiString::SubString()

1,097 views
Skip to first unread message

Michael Pawlowski

unread,
Feb 4, 2001, 1:22:41 PM2/4/01
to
Hello,

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


Rudy Velthuis (TeamB)

unread,
Feb 4, 2001, 1:51:58 PM2/4/01
to
Michael Pawlowski wrote in <3a7d9de1_1@dnews>...

> 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

Michael Pawlowski

unread,
Feb 4, 2001, 2:47:22 PM2/4/01
to

The index parameter for the SubString() method must be a 1 NOT 0 for the
first character of the string. I changed the index to 1, recompiled, and
the SubString() method executes as expected!!!

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...

Martin Stainsby

unread,
Feb 4, 2001, 3:33:28 PM2/4/01
to
CBuilder provides some routines for handling drive/path/ext

have a look at ExtractFilePath(const AnsiString FileName);

there are others too.


Rudy Velthuis (TeamB)

unread,
Feb 4, 2001, 3:40:39 PM2/4/01
to
Martin Stainsby wrote in <3a7dbc1a_1@dnews>...

> CBuilder provides some routines for handling drive/path/ext

ChangeFileExt() will do the trick:

TempString = ChangeFileExt(OpenDialog1->FileName, "");

0 new messages