Board index » delphi » Problem with array...

Problem with array...

Hello everybody
I have create an array of String.
I would like to know for a string how to search the index of array where
the string is.
For exemple, i want to know what is the position of the String 'dog' in
the array.
Thank you for your help
See you
Christine
 

Re:Problem with array...


function TMainForm.SearchArray(aString : string) : Boolean;
var
 I : Integer;
begin
 Result := False;
 for I := 0 to HIGH[YourArray] do
 begin
  if YourArray[I] = aString then
   begin
    Result := True;
    Exit;
   end;
 end;
end;

HTH

Rkr

Quote
Christine Margarit wrote:

> Hello everybody
> I have create an array of String.
> I would like to know for a string how to search the index of array where
> the string is.
> For exemple, i want to know what is the position of the String 'dog' in
> the array.
> Thank you for your help
> See you
> Christine

Re:Problem with array...


Quote
Christine Margarit wrote:
> Hello everybody
> I have create an array of String.
> I would like to know for a string how to search the index of array where
> the string is.
> For exemple, i want to know what is the position of the String 'dog' in
> the array.

    Have you considered using a TStringlist instead of an array of strings?
(TStringlist actually _is_ an array of strings, with a lot of useful stuff
added,
for example the IndexOf method tells you where a certain string is
located.)
Quote

> Thank you for your help
> See you
> Christine

Re:Problem with array...


Quote
In article <37B038AE.F3A46...@BULL.NET>, CHRISTINE.MARGA...@BULL.NET wrote:
>Hello everybody
>I have create an array of String.
>I would like to know for a string how to search the index of array where
>the string is.
>For exemple, i want to know what is the position of the String 'dog' in
>the array.
>Thank you for your help
>See you
>Christine

var
  myArray : array [1..10] of string;
  a, index : integer;
begin
  index := 0;
  for a := 1 to 10 do
  begin
    if myArray[a] = 'dog' then
      index := a;
  end;
  // now index either equals 0 or the position of 'dog'
end;

Alternately, try a TStringList.  For example:

var
  SL : TStringList;
begin
  SL := TStringList.create;
  SL.add('apple');
  SL.add('bike');
  SL.add('dog');
  SL.add('cat');

  index := SL.indexOf('dog');
  // index now equals 2 (the third position, it goes 0, 1, 2, ...) or -1 if it
  // doesn't exist

  //remember to free SL when you're finished with it
  SL.free;
end;

Stephen Legge
Step...@RightClick.ca

RightClick Softworks
Ottawa, Canada
www.RightClick.ca

Other Threads