?
Solved

how can i search if string exist in array ?

Posted on 2016-11-21
3
Medium Priority
?
98 Views
Last Modified: 2016-11-21
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

0
Comment
Question by:dolphin King
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 27

Expert Comment

by:Sinisa Vuk
ID: 41896680
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

0
 

Author Comment

by:dolphin King
ID: 41896692
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 ...
0
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 2000 total points
ID: 41896713
So, you don't need explode string as an array elements...
If you use Pos function?
,,,
if Pos(  ',' + 'Mark' + ',' , ',' + 'Mark,Michel,segment,' + ',') > 0 then
begin
 //...
end;

Open in new window


I concatenate separator to both strings at the start and the end of strings...
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question