TStringList.IndexOf: wildcard within indexof?
Asked Answered
W

3

6

I want to retrieve the linenumber in a stringlist (loaded from a file). Indexof seems to match exactly. Is there a way to retrieve the line with a wildcard-version of Indexof? something like SL.Indexof('?sometext')?

Thanks!

Withal answered 14/6, 2011 at 9:15 Comment(5)
@David the title changed, but the content is the same. Your answer is still valid.Arp
@johnny, TStrings is more proper class name. @martin, TStrings is very low-level class, for very basic array-like string manipulatilation. It is ideologicall incorect to overburden it with shell-style regexp.Deca
@user759588, the question concerns TStringList, not just any implementor of TStrings.Trejo
@johnny, IndexOf is TStrings functionality. Everyone use TStringList instead because TStrings is abstract class.Deca
@Downvoter, I know, I checked. Still I thought it would be easier to follow if the title and the question used the same words.Trejo
Y
9

If you want to match some part of the string, without any fancy wildcards, as you indicate in a comment to another answer, then you can use a simple function like this:

function FindMatchStr(Strings: TStrings; const SubStr: string): Integer;
begin    
  for Result := 0 to Strings.Count-1 do
    if ContainsStr(Strings[Result], SubStr) then
      exit;
  Result := -1;
end;

If you want a case-insensitive match then you can use this:

function FindMatchText(Strings: TStrings; const SubStr: string): Integer;
begin    
  for Result := 0 to Strings.Count-1 do
    if ContainsText(Strings[Result], SubStr) then
      exit;
  Result := -1;
end;

ContainsStr and ContainsText are defined in the StrUtils RTL unit and follow the standard convention of Str to indicate case sensitive comparison, and Text to indicate case insensitive.

Yeseniayeshiva answered 14/6, 2011 at 9:44 Comment(2)
hi david, thanks for your answer! i already solved it that way, so iterating through the string. i thought it would be nicer with a function that immediately returns the position.Withal
martin, have you ever looked at the implementation behind IndexOf? Ignoring the case when the stringlist is sorted (which wouldn't help you with your partial match anyway) IndexOf internally does exactly the same, iterate all strings and compare.Impurity
D
7

There's no built-in way to search TStringList for wildcards. You need to use a third-party library, such as TPerlRegEx for regular expressions.

Drusi answered 14/6, 2011 at 9:18 Comment(5)
Or use the built-in regex support in Delphi XEYeseniayeshiva
@Zruty: I think there are enough people out there not using XE to make your answer useful (+1). Besides the OP does not mention a specific Delphi version.Sore
I don't really need wildcards. It would also be nice if indexof would match some part of the string...Withal
@David: I know :) I would have voted you up as well, if this had been an answer.Sore
@martin: If you don't need wildcards then you can use RTL's Pos function to test does the line contain the substring you're looking for. Of course you can't use IndexOf but have to iterate over the lines yourself...Badge
C
0

Adapting David Heffernan's response for use with TStringList, the function could look like this:

function Util_StrLst_GetLineFromSubstr(iStrlst: TStringList; iSubstr: string): Integer;
begin
  for Result := 0 to iStrlst.Count-1 do
    if (Pos(iSubstr, iStrlst[Result]) > 0) then
      Exit;
  Result := -1;
end;
Cicada answered 18/4, 2020 at 17:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.