如何检查字符串是否包含Pascal中的字符串

use*_*179 8 pascal

我尝试了下面的代码,它应该检查一个字符串是否包含空格但是我得到一个错误.我怎么能检查一下

if Index('some string',' ')>1 then begin
   Result:= False;
 end
 else begin
      Result := True;  
 end;
Run Code Online (Sandbox Code Playgroud)

Far*_*lko 15

你可以使用pos功能.来自文档:

pos函数返回主字符串中子字符串的位置.如果主字符串中不存在子字符串,则返回的值将为0.

s:='note-book';
x:=pos('book',s); {x will be 6}
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到所有这些信息以及其他有用的提示

  • @NickG Pascal使用基于1的索引,而不是基于0的索引.因此,如果针头位于大海捞针的开头,则pos将返回1. (3认同)

Mus*_*mal 5

作为替代方案,AnsiContainsStr 可用于字符串包含操作。如果字符串包含给定的子字符串,则返回 True,否则返回 False。作为示例代码:

if AnsiContainStr(mainText, subText) then begin
   //enter here if mainText contains subText.
   //write code for doing needed operations here
end
Run Code Online (Sandbox Code Playgroud)