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!
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!
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.
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.
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;
© 2022 - 2024 — McMap. All rights reserved.