We help IT Professionals succeed at work.

Check if string has the right string

eNarc
eNarc used Ask the Experts™
on
Hi, how do I check if a string has a selected string in the string.

for example:

apples never fall short from a apple tree.

now if I wanted to search for only "Fall" it would bring back True. if I wanted to search for "Oranges" it would bring back false.

how could I put that into a function?

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
You could use the pos function.

if  pos('hello', 'well hello world') > 0 then showmessage('true');

in a function:
function stringcontains(innerstring, outerstring: string) : boolean;
begin
  result := pos(innerstring, outerstring) > 0;
end;
Top Expert 2010
Commented:
if AnsiContainsText('apples never fall short from a apple tree', 'Fall') then
  ShowMessage('got it');

Commented:
also a small tip, you can use 'trim' to trim off extra spaces or that might sit on either side of the strings
Ferruccio AccalaiSenior developer, analyst and customer assistance
Commented:
Aflarin is right.
Just in addition to him comment:
AnsiContainsText is CASE INSENSITIVE.

If you need the case sensitive stuff then use AnsiContainsStr from AnsiStrings.pas