delphi中pos函数的用法是什么

   2025-02-15 2990
核心提示:在Delphi中,Pos函数用于查找子字符串在给定字符串中的起始位置。其语法如下:function Pos(const SubStr: string; const Str: s

在Delphi中,Pos函数用于查找子字符串在给定字符串中的起始位置。其语法如下:

function Pos(const SubStr: string; const Str: string): Integer;

参数:

SubStr:要查找的子字符串。Str:要在其中查找子字符串的字符串。

返回值:

如果找到子字符串,则返回子字符串在字符串中的起始位置(从1开始计数)。如果未找到子字符串,则返回0。

示例用法:

var  Str: string;  SubStr: string;  PosIndex: Integer;begin  Str := 'Hello, world!';  SubStr := 'world';    PosIndex := Pos(SubStr, Str);  if PosIndex > 0 then    ShowMessage('Found at position: ' + IntToStr(PosIndex))  else    ShowMessage('Not found.');end;

在上述示例中,Pos(SubStr, Str)函数将返回子字符串"world"在字符串"Hello, world!"中的起始位置,即13。如果子字符串未找到,则返回0。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言