We help IT Professionals succeed at work.

Check if string has the right string

eNarc used Ask the Experts™
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?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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;
  result := pos(innerstring, outerstring) > 0;
Top Expert 2010
if AnsiContainsText('apples never fall short from a apple tree', 'Fall') then
  ShowMessage('got it');

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
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