Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search in TStrings

How can I check if a particular string exists in a TStrings ? For example I have TStrings which contain a lot of text and I want to check if string "Hello!" is present in this text.

"Hello!" is just an example string. It can be anything. String can be in between other strings like "something Hello! something"

like image 244
Santos Oliveira Avatar asked Dec 21 '22 11:12

Santos Oliveira


2 Answers

Use the pos function on the TStrings text property:

if pos('Hello!', strings.text) > 0 then
begin
end

This will find the string if it occurs anywhere in the TStrings. To find the string in which it occurs you would need to iterate through the strings applying the pos function on each of them.

like image 142
Keith Miller Avatar answered Dec 28 '22 23:12

Keith Miller


You can use the IndexOf function of TStrings

if Strings.IndexOf('Hello')<>-1 then
    caption:='Found';

This function return -1 if the string was not found, else it returns the index of this string in the TStrings;

like image 43
Zeina Avatar answered Dec 29 '22 01:12

Zeina