【问题标题】:Can I use wildcards to compare strings?我可以使用通配符来比较字符串吗?
【发布时间】:2020-06-03 10:30:42
【问题描述】:

我有一组文件:

001.txt
001a.txt
002.txt 
002a.txt
...

我正在尝试使用以下代码排除以a 结尾的项目,例如001a.txt

PROCEDURE TForm1.FindFiles(StartDir, FileMask: STRING);
VAR
  sr: TSearchRec;
  IsFound: Boolean;
BEGIN
  IsFound := FindFirst(StartDir + FileMask, faAnyFile - faDirectory, sr) = 0;
  WHILE IsFound DO
  BEGIN    
    if sr.Name <> '*a.*' then
      gFiles.add(StartDir + sr.Name);

    IsFound := FindNext(sr) = 0;
  END;
  FindClose(sr);
END;

传递给此过程的FileMask'*.*' 以包含所有文件。

但是上面返回所有文件。

所以我的问题是如何从搜索中排除这些文件?

【问题讨论】:

    标签: delphi wildcard delphi-10.3-rio


    【解决方案1】:

    Delphi 为此提供了单元System.Masks。这里合适的函数是MatchesMask

    if MatchesMask(sr.Name, '*a.*') then
    

    【讨论】:

    • 我用过这个但是倒过来从列表中排除文件。
    • 是的,我不知何故错过了 to exclude 部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-16
    • 1970-01-01
    • 2020-11-28
    • 1970-01-01
    • 2016-02-24
    相关资源
    最近更新 更多