TStringList.IndexOf:indexof中的通配符?

问题描述 投票:6回答:3

我想检索字符串列表中的行号(从文件中加载)。Indexof似乎与exactly匹配。有没有办法使用Indexof的通配符版本来检索行?类似于SL.Indexof('?sometext')?

谢谢!

delphi indexof
3个回答
9
投票

如果您要在字符串的某些部分进行匹配,而没有任何花哨的通配符,如对另一个答案的注释中所示,则可以使用像这样的简单函数:

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;

如果您想要不区分大小写的匹配,则可以使用此:

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;

[ContainsStrContainsTextStrUtils RTL单元中定义,并遵循Str的标准约定表示区分大小写,而Text表示不区分大小写。


7
投票

没有内置方法可在TStringList中搜索通配符。您需要使用第三方库,例如TPerlRegEx用于正则表达式。


0
投票

适应David Heffernan的响应以与TStringList一起使用,该函数可能如下所示:

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;
© www.soinside.com 2019 - 2024. All rights reserved.