Santos Oliveira
Santos Oliveira

Reputation: 497

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"

Upvotes: 4

Views: 7099

Answers (2)

Zeina
Zeina

Reputation: 1603

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;

Upvotes: 4

Keith Miller
Keith Miller

Reputation: 1768

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.

Upvotes: 8

Related Questions