Link to home
Start Free TrialLog in
Avatar of dolphin King
dolphin King

asked on

how can i search if string exist in array ?

i have this array of string

function Explode(const Separator, S: string; Limit: Integer = 0): TArray;
var
  SepLen       : Integer;
  F, P         : PChar;
  ALen, Index  : Integer;
begin
  SetLength(Result, 0);
  if (S = '') or (Limit < 0) then
    Exit;
  if Separator = '' then
  begin
    SetLength(Result, 1);
    Result[0] := S;
    Exit;
  end;
  SepLen := Length(Separator);
  ALen := Limit;
  SetLength(Result, ALen);

  Index := 0;
  P := PChar(S);
  while P^ <> #0 do
  begin
    F := P;
    P := StrPos(P, PChar(Separator));
    if (P = nil) or ((Limit > 0) and (Index = Limit - 1)) then
      P := StrEnd(F);
    if Index >= ALen then
    begin
      Inc(ALen, 5); //
      SetLength(Result, ALen);
    end;
    SetString(Result[Index], F, P - F);
    Inc(Index);
    if P^ <> #0 then
      Inc(P, SepLen);
  end;
  if Index < ALen then
    SetLength(Result, Index); //
end;


type
TArray = array of string;

.....

var
 arr: TArray;
begin
arr := explode(',', 'Mark,Michel,segment,');
//so this result will become arr[0] << Mark etc..
end;

Open in new window


as the code clearly shows that i can get arrays from arr variable
but i want to search in all arr

as example
if arr contain 'Mark' then 

do somthing ..

Open in new window

Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

I assume that Explode function is not yours. But anyway... here it is...
var
  i: Integer;
...
arr := explode(',', 'Mark,Michel,segment,');
for i := Low(arr) to High(arr) do
begin
  if arr[i] = 'Mark' then //or use common Pos function if like.... and you can compare UpperCase too...
  begin
     //do something....
     Break; //go out of loop
  end;
end;

Open in new window

Avatar of dolphin King
dolphin King

ASKER

i know that i can loop , i did not want to do that i was trying to search directly inside the arrays ,

explode functions are all over google search ...
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial