首页 > 解决方案 > 我可以使用通配符来比较字符串吗?

问题描述

我有一组文件:

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传递给此过程的是包含'*.*'所有文件。

但是,上面返回所有文件。

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

标签: delphiwildcarddelphi-10.3-rio

解决方案


德尔福System.Masks为此提供了单位。这里合适的功能是MatchesMask

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

推荐阅读