SRL Archive - SRL Snippets

Function: SearchString


kazhual

2012-10-28 17:23:14

Hello,

I was looking for a function which could search within a String, but i havent found anything so here is my solution for in case you are interested.

Example:

text1 := 'the bank is closed in 5 min';
text2 := 'bank';

SearchString(text1, text2);
this function will search for 'bank' in text1 and returns True of found.

I Hope you like it



program Development;
{$DEFINE SMART}
{$i srl/srl.simba}

function SearchString(s1,s2: string): Boolean;
var
i, j, k: Integer;
begin

Result := false;

for i:= 1 to ( length(s1)-length(s2)+1 ) do begin

if ( s1[i] = s2[1] ) then begin

dec(i);

for j:= 1 to length(s2) do begin

if ( s1[i+j] = s2[j] ) then begin

inc(k);

end;

if ( k = length(s2) ) then begin

Result := True;

end;

end;

inc(i);

end;

end;

end;

var
text1 : string;
text2 : string;
begin

text1 := 'the bank is closed in 5 min';
text2 := 'bank';

SearchString(text1, text2);
WriteLn( BoolToStr( SearchString(text1, text2) ) );

end.


J J

2012-10-28 17:27:01

Good that you made something yourself but we unfortunately/fortunately have a function already:

program New;
{$i SRL/srl.simba}

procedure Test;
var
s1, s2: String;

begin
s1 := 'hello my name is JJ';
s2 := 'name';

if Pos(s2, s1) > 0 then
writeln('s1 contains s2');
end;


begin
// Setting up Simba
SetupSRL;
Test;
end.

Compiled successfully in 452 ms.
SRL Compiled in 0 msec
s1 contains s2
Successfully executed.

kazhual

2012-10-28 17:31:24

well to bad after all,

luckily didn't cost me that mucht time.

But i was looking right here -> http://docs.villavu.com/simba/scriptref/string.html <-

Didn't found any useable function, and the function POS is not subscribed there.