RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.StrRScan Function

Returns a pointer to the last occurrence of a specified character in a string.

Pascal
function StrRScan(const Str: PAnsiChar; Chr: AnsiChar): PAnsiChar; overload;
function StrRScan(const Str: PWideChar; Chr: WideChar): PWideChar; overload;
C++
PAnsiChar StrRScan(const PAnsiChar Str, AnsiChar Chr);
PWideChar StrRScan(const PWideChar Str, WideChar Chr);

StrRScan returns a pointer to the last occurrence of Chr in Str.  

If Chr does not occur in Str, StrRScan returns nil (Delphi) or NULL (C++). The null terminator is considered to be part of the string.

Note: If the source string contains international characters, use AnsiStrRScan instead.
 

C++ Examples: 

 

/*
The following example uses an Open dialog box and a button
on a form. When the button is clicked, the user is prompted
for a filename. Then the filename, minus its path, is
displayed in a message box.
Note:   This technique will not work with multi-byte
character sets (MBCS). To be more general, use ExtractFileName.
*/
char *__fastcall GetFileName(char *pszFilePath)
{
  char *pszFileName = StrRScan(pszFilePath, '\\');
  if (!pszFileName)
    pszFileName = pszFilePath;
  else
    pszFileName++;
  return pszFileName;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  if (OpenDialog1->Execute())
  {
    char *name = GetFileName(OpenDialog1->FileName.t_str());
    Application->MessageBox(
      WideString(name).c_bstr(),
      L"File Name",
      MB_OK);
  }
}

 

Delphi Examples: 

{
This function behaves much like ExtractFileName except that
it uses null-terminated strings and it will not work with
multi-byte character sets (MBCS).
}
function NamePart(FileName: PAnsiChar): PAnsiChar;
 var
  P: PAnsiChar;
  sep: Char;
begin
  sep := '/';
  P := StrRScan(FileName, '/');
  if P = nil then
  begin
    P := StrRScan(FileName, ':');
    if P = nil then P := FileName;
  end;
  NamePart := P;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  S : string;
begin
  S := string(NamePart('C:\Test.fil'));
  Canvas.TextOut(10, 10, S);
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!