Mega Code Archive

 
Categories / Delphi / Strings
 

Finding a word in a string

Title: Finding a word in a string Question: N/A Answer: Somethimes it is usefull to some words into a string so you only have to need one variable. In most circomstances the variable is set to an inifile or in the registry. This is an example how to get a word and store it in a variable: var FoundStr : string; begin FoundStr := FindStr('This is an example',3,' '); if FindStr('red,yellow,blue',2,',') = 'blue' then .... else ....; end; function FindStr(S : string; I : integer; Sign : string) : string; var teller,t,t1,t2 : shortint; begin S := Sign + S + Sign; teller := 1; t := 0; while t begin if Copy(S,teller,1) = Sign then Inc(t); inc(teller); end; {while teller} t1 := teller; if Copy(S,teller,1) = Sign then result := '' else begin Inc(teller); while Copy(S,teller,1) Sign do Inc(teller); t2 := teller - t1; result := copy(S,t1,t2); end; {if} end;