Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if string contains string in Pascal

Tags:

pascal

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;
like image 466
user1016179 Avatar asked Feb 26 '14 16:02

user1016179


2 Answers

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

like image 125
Farkhat Mikhalko Avatar answered Oct 18 '22 20:10

Farkhat Mikhalko


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
like image 27
Mustafa Kemal Avatar answered Oct 18 '22 19:10

Mustafa Kemal