> 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
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
>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
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
> 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.
> 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.
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.
> 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!