【问题标题】:How to search the first occurrence from two different tstringlist如何从两个不同的 tstringlist 中搜索第一个匹配项
【发布时间】:2021-12-01 09:14:36
【问题描述】:

我需要比较两个不同的列表,其中包含需要从路径(chosenDirectoryFOpenDialog.FileName)复制到我的本地存储库(PathLocalRepo = ExtractFilePath(Application.ExeName)+LOCAL_REPOSITORY_01)的文件夹名称。

问题是我需要检查一个文件夹名称是否已经存在于我的本地存储库中,如果存在则我需要转义我的循环并继续下一个值。

这是我的代码:

for N := 0 to ListFoldersImported.Count-1 do
begin
  for Y := 0 to ListFoldersLocalRepository.Count-1 do
  begin
    if MatchStr(ExtractFileName(ListFoldersImported[N]), ExtractFileName(ListFoldersLocalRepository[Y])) = True then
    begin
      FlagFound := True;
      Break;
    end else
    begin
      FlagFound := False;
      ListOfFoldersThatNeedsToBeCopied.Add(ListFoldersImported[N]);
    end;
  end;
  if FlagFound = False then
    TDirectory.Copy(chosenDirectory,PathLocalRepo)
end;

如果找到第一个匹配项,此代码不会停止我的循环,并且不会继续将 [Y] 与下一个 [N] 值进行比较。

【问题讨论】:

    标签: delphi pascal delphi-2010


    【解决方案1】:

    也许你需要下一个逻辑:

    for N:= 0 to ListFoldersImported.Count-1 do
    begin
        FlagFound := False;
        for Y:= 0 to ListFoldersLocalRepository.Count-1 do
        begin
              if MatchStr(ExtractFileName(ListFoldersImported[N]),ExtractFileName(ListFoldersLocalRepository[Y])) = True then
              begin
                  FlagFound := True;
                  Break;
              end;
        end;
        if not FlagFound then
        begin
           ListOfFoldersThatNeedsToBeCopied.Add(ListFoldersImported[N]);
           TDirectory.Copy(chosenDirectory,PathLocalRepo);
        end;
     end;
    

    【讨论】:

    • 效果很好!!!我的错误是关闭嵌套的 for 循环以比较固定值与左右值每次变化
    猜你喜欢
    • 2013-12-21
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多