RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.StrPos Function

Returns a pointer to the first occurrence of STR2 in STR1.

Pascal
function StrPos(const Str1: PAnsiChar; const Str2: PAnsiChar): PAnsiChar; overload;
C++
PAnsiChar StrPos(const PAnsiChar Str1, const PAnsiChar Str2);

StrPos returns a pointer to the first occurrence of Str2 in Str1. If Str2 does not occur in Str1, StrPos returns nil (Delphi) or NULL (C++).

Note: If the source string contains international characters, use AnsiStrPos instead.
Delphi Examples:

{
The following example uses two edit controls and a button on
a form. When the button is clicked, the text in the first
edit control is searched for the text in the second edit control.
}
procedure TForm1.Button1Click(Sender: TObject);
begin
      if StrPos(PChar(Edit1.Text), PChar(Edit2.Text)) <> nil then
         ShowMessage('Substring found')
      else
         ShowMessage('Substring not found');
end;

C++ Examples:

/*
The following example uses two edit controls and a button on
a form. When the button is clicked, the text in the first
edit control is searched for the text in the second edit control.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
      if (StrPos(Edit1->Text.c_str(), Edit2->Text.c_str()))
         ShowMessage("Substring found.");
      else
         ShowMessage("Substring not found.");
}

 

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