How to check if string contains string in Pascal

19,741

Solution 1

You can use pos function. From documentation:

The pos function returns the position of a substring in a main string. If the substring does not exist in the main string, then the returned value will be 0.

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

All this information, and other useful tips you can find here

Solution 2

As an alternative one, AnsiContainsStr can be used for string containing operations.It return True if the string contains the given substring, False otherwise. As an example code:

if AnsiContainStr(mainText, subText) then begin
   //enter here if mainText contains subText.
   //write code for doing needed operations here
end
Share:
19,741
user1016179
Author by

user1016179

Updated on June 27, 2022

Comments

  • user1016179
    user1016179 almost 2 years

    I tried the following code which supposed to check if a string contains spaces but I get an error. How else can I check that

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