Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Checking a set of strings

348 views
Skip to first unread message

Joanna Carter (TeamB)

unread,
May 27, 2005, 4:07:06 AM5/27/05
to
"Tom Dalton" <tom.d...@eskom.co.za> a écrit dans le message de news:
4296d3d2$1...@newsgroups.borland.com...

> I have tried to set an Array of string.
> CGases : Array of String;
> But when I set
> CGases[0] := 'ACETYLENE'; I get an error.

You are geting an error because you have not set the length of the array
before using it.

> I have also tried using TStringlist component with no luck. I have checked
out the help files but I have not seen direction on how to do this.

Try this

var
strs: TStrings;
begin
strs := TStringList.Create;
try
if strs.IndexOf(testStr) > -1 then
// testStr is in the list
...
finally
strs.Free;
end;

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker


Tom Dalton

unread,
May 27, 2005, 4:01:22 AM5/27/05
to

Hi,

I have try a few ways to do this but I seem to be comming up with dead ends!

I have a set of strings ['ACELYLENE','ETHANE','ETHYLENE',etc] now I would like test them against a variable that is obtained from another source.

So we can go the route of:
IF (Comp = 'ACETYLENE') or (Comp = 'ETHANE') or .... then

But is there a better method of checking whether Comp is in the set of parameters?

I have tried to set an Array of string.
CGases : Array of String;
But when I set
CGases[0] := 'ACETYLENE'; I get an error.

I have also tried using TStringlist component with no luck. I have checked out the help files but I have not seen direction on how to do this.

Regards

Tom Dalton

Anders Isaksson

unread,
May 27, 2005, 8:29:10 AM5/27/05
to
On Fri, 27 May 2005 09:07:06 +0100, "Joanna Carter \(TeamB\)"
<joa...@nospam.co.uk> wrote:

>Try this
>
>var
> strs: TStrings;
>begin
> strs := TStringList.Create;

strs.Text := 'ACELYLENE'#10'ETHANE'#10'ETHYLENE'#10...';

> try
> if strs.IndexOf(testStr) > -1 then
> // testStr is in the list
> ...
> finally
> strs.Free;
> end;

--
Anders Isaksson, Sweden
BlockCAD: http://web.telia.com/~u16122508/proglego.htm
Gallery: http://web.telia.com/~u16122508/gallery/index.htm

Peter Below (TeamB)

unread,
May 27, 2005, 10:37:09 AM5/27/05
to
In article <4296d3d2$1...@newsgroups.borland.com>, Tom Dalton wrote:
> I have try a few ways to do this but I seem to be comming up with dead ends!
>
> I have a set of strings ['ACELYLENE','ETHANE','ETHYLENE',etc] now I would like test them against a variable that is obtained from another source.
>
> So we can go the route of:
> IF (Comp = 'ACETYLENE') or (Comp = 'ETHANE') or .... then
>
> But is there a better method of checking whether Comp is in the set of parameters?

As Joanne said in her reply putting the strings into a Tstringlist and using
its IndexOf method is one approach. For a set of literal strings you may find this
little routine useful, though:

{: Tests if aString is in the passed array of strings
@Param aString is the string to test for
@Param A is an array of strings to test against
@Param caseSensitive determines whether the test is case-sensitive
or not
@Returns true if aString is one of the strings in the array, false if
not. }
function StringIn(const aString: string;
const A: array of string;
caseSensitive: Boolean = true): Boolean;
var
i: Integer;
begin { StringIn }
Result := false;
for i := Low(A) to High(A) do begin
if caseSensitive then
Result := AnsiCompareStr(aString, A[i]) = 0
else
Result := AnsiCompareText(aString, A[i]) = 0;
if Result then
Break;
end; { For }
end; { StringIn }

Use is like this:

if StringIn(Comp, ['ACELYLENE','ETHANE','ETHYLENE']) then
....

You can also pass a array-of-string constant to this function.

--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


Tom Dalton

unread,
May 29, 2005, 4:12:58 PM5/29/05
to

"Joanna Carter \(TeamB\)" <joa...@nospam.co.uk> wrote:
Thanks a million Joanna I will try this tomorrow. I cannot try it from home as I am using an Oracle Server to get data. But thanks again.
Regards
Tom Dalton

Anthony Frazier

unread,
Jun 2, 2005, 12:09:30 PM6/2/05
to
Joanna Carter (TeamB) wrote:

> if strs.IndexOf(testStr) > -1 then
> // testStr is in the list

Sorting the list and using the Find method may be better if lots of
lookups need done. Find uses a binary search versus IndexOf's linear
search.

--
Pax tecum,

Anthony Frazier
Victor Printing, Inc.

Anthony Frazier

unread,
Jun 2, 2005, 12:16:47 PM6/2/05
to
Peter Below (TeamB) wrote:

> As Joanne said in her reply putting the strings into a Tstringlist and using
> its IndexOf method is one approach. For a set of literal strings you may find this
> little routine useful, though:

This looks a lot like AnsiMatchText/AnsiMatchStr, which are available in
Delphi 7 (at least, maybe earlier?). In Delphi 2005 there's now
MatchText/MatchStr. Right now they're only inline calls to the Ansi-
functions, but who knows what they may become in future versions.

There are also AnsiIndexText/AnsiIndexStr if you want the position in
the array. (Good for case statements and the like.)

These should all be in StrUtils.

Wayne Niddery [TeamB]

unread,
Jun 2, 2005, 4:34:43 PM6/2/05
to
Anthony Frazier" <"afrazier AT victorptg DOT com wrote:
>
> Sorting the list and using the Find method may be better if lots of
> lookups need done. Find uses a binary search versus IndexOf's linear
> search.

If the Sorted property is true then IndexOf calls Find itself, so no need to
test for which method to call.

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"True peace is not the absence of tension, but the presence of
justice." - Martin Luther King, Jr.


Anthony Frazier

unread,
Jun 3, 2005, 3:02:58 PM6/3/05
to
Wayne Niddery [TeamB] wrote:

> If the Sorted property is true then IndexOf calls Find itself, so no need to
> test for which method to call.

::Peeks at VCL Source:: Ah! Thanks!

0 new messages