Jump to content
robertjohns

Find String in TstringList

Recommended Posts

I am trying to find find Strings in TstringList

I used almost all method but nothing working

 

In StringList I have

 

some

some.pds

other

other.cff

--

 

I am trying to find some.pds ad other.cff .. Result is True

 

but If in Stringlist

 

some

other

--

 

and still I am trying to find some.pds ad other.cff .. but still Result is True whereas it should return false

 

How can I find Specific string in Tstringlist

Share this post


Link to post
8 minutes ago, Lars Fosdal said:

Perhaps  you should show us the search code you use?

for I := 0 to strList.Count -1 do
if (Pos('some.pds',strList[i])>0) or
(Pos('other.cff',strList[i])>0) Then
Result:=True else Result:=False;

 

for I := 0 to strList.Count -1 do
If (strList[I].Contains('some.pds')) or
(strList[I].Contains('other.cff')) Then
Result:=True else Result:=False;

 

for I := 0 to strList.Count -1 do
If (SameText('some.pds',strList[i])) or
(SameText('other.cff',strList[i])) 
Then
Result:=True else Result:=False;

 

function ContainsStr(const AStrings : TStrings; const ASearchStr : String) : Boolean;
var
  LIdx : Integer;
begin
  Result := Pos( ASearchStr, AStrings.Text ) > 0;
  if Result then
  begin
    for LIdx := 0 to AStrings.Count - 1 do
      if Pos( ASearchStr, AStrings[LIdx] ) > 0 then
        Exit;
    Result := False;
  end;
end;

 

Share this post


Link to post

The first three can fail because you are resetting Result for each entry, meaning only the last line will decide the result.

Result := False;
for var s in StrList
do begin 
  Result := Result or (SameText(s, 'some.pds') or SameText(s, 'other.cff'));
  if Result
  then Break;
end;

Is there whitespace in the strList entries? If there is - Trim.

 

Why the double search in ContainsStr?

Is that the AnsiStrings.SameString or the SysUtils.SameString?

 

Share this post


Link to post
function ContainsStr(const AStrings : TStrings; const ASearchStr : String) : Boolean;
var
  LIdx : Integer;
begin
  Result := Pos( ASearchStr, AStrings.Text ) > 0;
  if Result then
  begin
    for LIdx := 0 to AStrings.Count - 1 do
      if Pos( ASearchStr, AStrings[LIdx] ) > 0 then
        Exit;
    Result := False;
  end;
end;

This function does the job but it find matching strings not the exact search strings

 

If StringList have

 

some

other

 

and if Search strings are some.pds ad other.cff , Result is always True because it matches the strings with some and other whereas I expect it to find some.pds ad other.cff , Result should return False

 

Share this post


Link to post
4 hours ago, robertjohns said:

Thanks but Same issue as previous attempts

Have you checked the data in the string list for non-printable characters around the .extensions?

Share this post


Link to post
1 hour ago, Lars Fosdal said:

Have you checked the data in the string list for non-printable characters around the .extensions?

There are no non-printable characters around extensions

Share this post


Link to post
program Project12;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.Classes, System.SysUtils;

function ContainsString(const AStringList: TStringList; const ASearchFor: string): Boolean;
begin
  Result := (AStringList.IndexOf(ASearchFor) <> -1);
end;

var
  sl: TStringList;
begin
  try
    sl := TStringList.Create;
    try
      sl.Sorted := False;
      sl.Duplicates := dupIgnore;
      sl.CaseSensitive := True;
      sl.Add('some');
      sl.Add('some.xyz');
      WriteLn(ContainsString(sl, 'some')); // True
      WriteLn(ContainsString(sl, 'Some')); // False
      WriteLn(ContainsString(sl, 'yxz'));  // False
    finally
      sl.Free;
    end;
    ReadLn;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

 

Share this post


Link to post
11 hours ago, robertjohns said:

How can I find Specific string in Tstringlist

procedure TForm1.Button1Click(Sender: TObject);
var
  LFind      : TArray<string>;
  LWhereIsIt : TStringList;
  LFound     : integer;
  LItemsFound: TArray<string>;
begin
  LFind       := ['some.pds', 'other.cff', 'world.exe'];
  LItemsFound := [];
  //
  LWhereIsIt := TStringList.Create;
  try
    LWhereIsIt.AddStrings(['hello.tt', 'other.cff', 'world.exe', 'delphi.dp', 'some.pds']);
    //
    LWhereIsIt.Sort; // mandatory to "StringList.Find()" -> many functions needs "a sorted list"
    //
    for var F in LFind do
      if LWhereIsIt.Find(F, LFound) then
        LItemsFound := LItemsFound + [F];
    //
    if (length(LItemsFound) > 0) then
      begin
        if (length(LItemsFound) = length(LFind)) then
          Memo1.Lines.Add('all items was found: ' + ''.join(',', LItemsFound))
        else
          Memo1.Lines.Add('some/all item(s) was not found: ' + ''.join(',', LItemsFound));
      end
    else
      Memo1.Lines.Add('nothing was found');
  finally
    LWhereIsIt.Free;
  end;
end;

 

  LWhereIsIt := TStringList.Create;
  try
    LFind := ['some.pds', 'other.cff', 'world.exe'];
    LWhereIsIt.AddStrings(['hello.tt', 'other.cff', 'world.exe', 'delphi.dp', 'some.pds']);
    LHowManyItemsFound := 0;
    //
    for var F in LFind do
      if LWhereIsIt.DelimitedText.Contains(F) then
        LHowManyItemsFound := LHowManyItemsFound + 1;
    //
    if (length(LFind) = LHowManyItemsFound) then
      Memo1.Lines.Add('all items was found');
  finally
    LWhereIsIt.Free;
  end;

 

Edited by programmerdelphi2k

Share this post


Link to post
15 hours ago, KodeZwerg said:

IndexOf(ASearchFor)

Isn't that case sensitive?

Share this post


Link to post
15 hours ago, KodeZwerg said:

      sl.CaseSensitive := True;

 

24 minutes ago, Lars Fosdal said:

Isn't that case sensitive?

 

Take a guess 😛

Share this post


Link to post

Right, so it should be set to false to match the original code samples.

 

There is something odd in the original samples.

If I copy the text, and paste it into a code block here - I get artifacts in other.cff - note the red dots.

Makes me wonder if the artifacts also exist in the original code - in which case it should fail.

 

image.thumb.png.b9c9245593b8b53b8d02efa48e9ae38b.png

Share this post


Link to post

@Lars Fosdal
In first post he tried to search for something, than he wanted a search for specific things, that will my code do.

Share this post


Link to post

@programmerdelphi2k

 

 

LFind := ['micro.pds', 'other.cff', 'world.exe'];
LWhereIsIt.AddStrings(['hello.tt', 'other.cff', 'world.exe', 'delphi.dp', 'some.pds']);

 

In this case it doesn't work

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×