SRL Archive - SRL Archives

function PosExx(substrarr: TStringArray; str: string): integer;


marpis

2009-05-27 09:03:59

It cheks if some of the strings in substrarr is found on str, and returns the position. Example:
PosExx(['ones', 'hicken'], GetUpText)

It's not case-sensitive.
I have found this function very useful in many ways

function PosExx(substrarr: TStringArray; str: string;var index: integer): integer;
var
i: integer;
begin
for i:= 0 to High(substrarr) do
begin
result:= pos(lowercase(substrarr[i]), lowercase(str));
if (result > 0) then
begin
index := i;
exit;
end;
end;
end;

Edited to return index too

ZaSz

2009-05-27 09:11:10

You made it so you have to do lowercase. If you type uppercase it will lowercase and boom. Wont find a capital in uptext. So it is case sensitive.

Smartzkid

2009-05-27 09:12:21

Wouldn't it be more logical to return an array?

EDIT @ zasz: it lowercases both sides of the comparison..

Blumblebee

2009-05-27 09:12:52

From: zasz
You made it so you have to do lowercase. If you type uppercase it will lowercase and boom. Wont find a capital in uptext. So it is case sensitive.


but its faster to not look for a capital in uptext anyways?

ZaSz

2009-05-27 09:13:52

From: Smartzkid
Wouldn't it be more logical to return an array?

EDIT @ zasz: it lowercases both sides of the comparison..


O i see.

senrath

2009-05-27 09:25:35

What's the point of returning the position in the string within the string array that the search string was found in?

Da 0wner

2009-05-27 10:40:05

This would help if you knew what index of the array was found…

Quickmarch

2009-05-27 11:08:33

This isn't a bad function, in fact i use a similar thing in almost every one of my scripts(Usually in forms), or to find an index of a point in a TPointArray.

marpis

2009-05-27 14:09:33

From: Smartzkid
Wouldn't it be more logical to return an array?


the use would become too complicated in simple tasks

Da 0wner

2009-05-27 15:05:23

Okay I was about to edit and add index, but you already did :p, just didn't say so.

function PosExx(substrarr: TStringArray; str : string; CaseSensitive : boolean): TIntegerArray;
var
i, h : integer;
begin
h := high(SubStrArr);
SetArrayLength(Result, h + 1);
for i := 0 to h do
begin
if not CaseSensitive then
result[i] := pos(lowercase(str), lowercase(SubStrArr[i]))
else
result[i] := pos(str, SubStrArr[i]);
if result[i] = 0 then
SetArrayLength(Result, GetArrayLength(Result) - 1);
end;
end;
:D

marpis

2009-05-27 15:11:07

From: Blumblebee
but its faster to not look for a capital in uptext anyways?


i think case-sensitive would be faster because in my function i call lowercase() 2 times for every string. but this is unessential, because we are talking about microseconds here

Da 0wner

2009-05-27 15:25:11

Marpis, try my function. Returns an array and case-sensitive is an option.

Smartzkid

2009-05-27 23:20:51

From: marpis
the use would become too complicated in simple tasks


Not really, you just have to know how to use it correctly ;)

Plus, it has some major advantages:

You can use it to detect multiple strings at once

OR

You can use it to detect if any one string was found

OR

You can use it to detect how many strings were found

OR

…