Returns the index value of the first character in a specified substring that occurs in a given string.
function Pos(const substr: AnsiString; const str: AnsiString): Integer; overload; function Pos(const substr: WideString; const str: WideString): Integer; overload;
int Pos(const AnsiString substr, const AnsiString str); int Pos(const BSTR substr, const BSTR str);
System
In Delphi, Pos searches for a substring, Substr, in a string, S. Substr and S are string-type expressions.
Pos searches for Substr within S and returns an integer value that is the index of the first character of Substr within S. Pos is case-sensitive. If Substr is not found, Pos returns zero.
The PosEx function is similar to Pos, but provides additional features and can be used in C++ code.
Delphi Examples:
{ The following example updates the strings in a list box given the strings contained in another list box. If a string in the source list box has the form Name=Value and the destination list box contains a string with the same Name part, the Value part in the destination list box will be replaced by the source’s value. TListBox objects that are not name-value pairs are not assigned a Name and IndexOfName will return -1. } procedure MergeStrings(Dest, Source: TStrings); var I, DI: Integer; begin for I := 0 to Source.Count - 1 do begin if Pos ('=', Source[I]) > 1 then begin DI := Dest.IndexOfName(Source.Names[I]); if DI > -1 then Dest[DI] := Source[I]; end; end; end; procedure TForm1.Button1Click(Sender: TObject); begin MergeStrings(ListBox1.Items, ListBox2.Items); end; procedure TForm1.FormCreate(Sender: TObject); begin ListBox1.Items.Add('Plants = 10'); ListBox1.Items.Add('Animals = 20'); ListBox1.Items.Add('Minerals = 15'); ListBox2.Items.Add('Animals = 4'); ListBox2.Items.Add('Plants = 3'); ListBox2.Items.Add('Minerals = 78'); end;
{ This example demonstrates how to edit particular elements of a string. } procedure TForm1.Button1Click(Sender: TObject); var S: string; begin S := Edit1.Text; { Convert spaces to zeros } while Pos(' ', S) > 0 do S[Pos(' ', S)] := '0'; Edit1.Text := S; end;
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|