StrPos,StrScan,

 

Delphi提供的字符串函数里有一个Pos函数,它的定义是:

function Pos(Substr: string; S: string): Integer;

它的作用是在字符串S中查找字符串Substr,返回值是Substr在S中第一次出现的位置,如果没有找到,返回值为0。

使用pos函数来查找字符第一次出现的位置
var
str1:string;
i,j:integer;
begin
str1:='dsf4654f6<ds>ad' ;
j:=pos('<',str1);//在字符串str1中查找"<"
if j<>0 then //得到的j是字符串中出现的位置,是整型
showmessage('<'+'在第'+inttostr(j)+'个位置'); //第十个位置
end;

===============================================

首部 function StrPos(const Str1, Str2: PChar): PChar; $[SysUtils.pas
功能 返回指针字符串Str2在Str1中第一个出现的地址
说明 没有找到则返回空指针;StrPos('12345', '3') = '345'
参考 <NULL>
例子 Edit3.Text := StrPos(PChar(Edit1.Text), PChar(Edit2.Text));

===================================================

StrScan : 返回一个字符在一个 PChar 串中第一次出现的位置指针;
StrRScan : 返回一个字符在一个 PChar 串中最后一次出现的位置指针;
StrPos : 返回一个 PChar 串在另一个 PChar 串中第一次出现的位置指针.

 

unit Unit1;  
  
interface  
  
uses  
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  Dialogs, StdCtrls;  
  
type  
  TForm1 = class(TForm)  
    Button1: TButton;  
    Button2: TButton;  
    procedure Button1Click(Sender: TObject);  
    procedure Button2Click(Sender: TObject);  
  end;  
  
var  
  Form1: TForm1;  
  
implementation  
  
{$R *.dfm}  
  
//StrScan、StrRScan:  
procedure TForm1.Button1Click(Sender: TObject);  
var  
  p: PChar;  
  str: string;  
begin  
  str := 'ABCBBBCBA';  
  p := StrScan(PChar(str), 'B');  
  ShowMessage(p);  {BCBBBCBA}  
  
  p := StrRScan(PChar(str), 'B');  
  ShowMessage(p);  {BA}  
end;  
  
//StrPos:  
procedure TForm1.Button2Click(Sender: TObject);  
var  
  p: PChar;  
  str: string;  
begin  
  str := '123456789';  
  p := StrPos(PChar(str), '456');  
  ShowMessage(p); {456789}  
end;  
  
end.  

 

http://blog.csdn.net/yunqian09/article/details/5310422

 

posted @ 2016-05-22 06:18  findumars  Views(1179)  Comments(0)    收藏  举报