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

如何检查字符串是否包含Pascal中的字符串,pascal,Pascal,我尝试了下面的代码,它应该检查字符串是否包含空格,但我得到了一个错误。 我还能怎么查呢 if Index('some string',' ')>1 then begin Result:= False; end else begin Result := True; end; 您可以使用pos功能。从文件: pos函数返回子字符串在主字符串中的位置。 如果主字符串中不存在子字符串,则返回 值将为0 所有这些信息以及您可以找到的其他有用提示作为替代,AnsiCont

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

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

您可以使用pos功能。从文件:

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


所有这些信息以及您可以找到的其他有用提示作为替代,AnsiContainsStr可用于包含字符串的操作。如果字符串包含给定的子字符串,则返回True,否则返回False。作为示例代码:

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

在这种情况下,如果字符串位于最开始的位置,您如何知道它是否包含该字符串?如果未找到子字符串,结果将与相同。@NickG Pascal使用基于1的索引,而不是基于0的索引。因此,如果指针位于干草堆的起点,pos将返回1。
if AnsiContainStr(mainText, subText) then begin
   //enter here if mainText contains subText.
   //write code for doing needed operations here
end