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

时间:2014-02-26 16:39:43

标签: pascal

我尝试了以下代码,该代码应该检查字符串是否包含空格但是我收到错误。 我怎么能检查

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

2 个答案:

答案 0 :(得分:14)

您可以使用 pos 功能。来自文档:

  

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

s:='note-book';
x:=pos('book',s); {x will be 6}

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

答案 1 :(得分:3)

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

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