Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I find specific text in a TSringList? 2

Status
Not open for further replies.

doctorjellybean

Programmer
May 5, 2003
145
I've been Googling for 2 days and can't seem to find a suitable solution.

The Stringlist will contain strings similar to the following:

D:\_DAZContent\DSClothing\Clothing\Runtime\Textures\Rhiannon\Sophie\Sophie_torso.jpg
D:\_DAZContent\DSClothing\Clothing\Runtime\Textures\Rhiannon\Sophie\Sophie_torsobum.jpg
D:\_DAZContent\DSClothing\Clothing\Runtime\Textures\Rhiannon\Sophie\Sophie_torsoS.jpg
D:\_DAZContent\DSClothing\Clothing\SophieLicense.txt
D:\_DAZContent\DSClothing\Clothing\SophieReadme.txt

What I need to do is see if Runtime (upper or lowercase) exists. Runtime could be anywhere in the stringlist i.e. beginning/middle/end of a string. I just need to check that it actually exists in the stringlist.

An example would be greatly appreciated.

Thanks in advance.
 
what about AnsiContainsText(Strlist.Text, 'runtime') ??

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I tried that but couldn't get it to work (maybe wrong usage). Couldn't find any examples of it either.
 
This function works, show us some actual code then...


/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I apologize for being "dim", but I have never used AnsiContainsText before. So I've Googled for it and I'm trying it out as I go along. According to an example (on Delphi Basics):


function AnsiContainsText ( const Haystack, Needle : string ) : Boolean;
var
haystack : AnsiString;
begin
haystack := 'The cat sat on the mat';

// Note that AnsiContainsStr is case insensitive
if AnsiContainsStr(haystack, 'THE')
then ShowMessage('''THE'' was found')
else ShowMessage('''THE'' was not found');

if AnsiContainsStr(haystack, 'the')
then ShowMessage('''the'' was found')
else ShowMessage('''the'' was not found');
end;

Delphi then states
[DCC Error] Unit1.pas(26): E2004 Identifier redeclared: 'haystack'

I admit that when it comes to functions, my knowledge is very basic. Hence using an example (like above) to see how it works and then adapt as needed. So I guess that I'm using the example the wrong way.
 
Ok, I think I have figured out why the above doesn't work (removed the var declaration of haystack and not assigning a value to Haystack inside the function). Will work it out some more over the weekend. Thanks for pointing me out in the right direction.
 
AnsiContainsText is the case insensitive version of AnisContainsStr.

your code should be simple:

Code:
uses StrUtils, ...

...
function StringListContainsRuntime(StrList : TStringList) : Boolean;
begin
 Result := AnsiContainsText(StrList.Text, 'runtime');
end;

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I can't believe that it is that simple! It works perfectly

Code:
function StringListContainsRuntime(StrList : TStringList) : Boolean;
begin
Result := AnsiContainsText(StrList.Text, 'runtime');
if result=true then showmessage('Yes');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
StrList : TStringList;
begin
StrList := TStringList.Create;
StrList.LoadFromFile('D:\Test.txt');
StringListContainsRuntime(StrList);
end;

Many thanks!
 
dont know if you already knew this
having the function that way isnt realy being a function
it should be...
Code:
function StringListContainsString(StrList:TStringList;SearchString:String):Boolean;
begin  
  Result := AnsiContainsText(StrList.Text, SearchString);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  StrList : TStringList;
begin
  StrList := TStringList.Create;
  StrList.LoadFromFile('D:\Test.txt');
  if StringListContainsRuntime(StrList,'runtime') then
    Showmessage('Found it');
end;

Aaron
 
This is how it finally ended, taylor.

Code:
function StringListContainsRuntime(StrList : TStringList) : Boolean;
begin
Result := (Pos('\Runtime\', StrList.Text) > 0);
End;

procedure TForm1.Button1Click(Sender: TObject);
var
StrList : TStringList;
begin
StrList := TStringList.Create;
StrList.LoadFromFile('D:\Test.txt');
StringListContainsRuntime(StrList);
if StringListContainsRuntime(StrList)=false then begin
showmessage('no');
exit end else
.....continue with rest of process
end;

However, yours is more compact :) Thank you.
 
your processing the string list twice

Here *** StringListContainsRuntime(StrList);
And
Here *** if StringListContainsRuntime(StrList)=false then begin

remove the first one.

why even have the function unless you intend to pass lots of string lists.

Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  StrList : TStringList;
begin
  StrList := TStringList.Create;StrList.LoadFromFile('D:\Test.txt');
  if not (Pos('\Runtime\', StrList.Text) > 0) then
    begin
      showmessage('no');
      exit
    end
  else.....continue with rest of processend;
[\code]


Aaron
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top