先定义了这个变量MyStrList: TStringList;
然后用mystrlist.add()往里面加了很多文件名,如abc.exe、xyz.exe等等,
如果里面有重复的文件名,如abc.exe、xyz.exe、abc.exe,怎样让它只保留一个呢?
事先不知道里面有多少个重复的文件名在线等待!谢谢!

解决方案 »

  1.   

    MyStrList.Duplicates := dupIgnore;
    这样Add的时候就会将同名的忽略掉。
      

  2.   

    if SL.IndexOf(S) < 0 then SL.Append(Uppercase(S));
    大写是为了避免Abc.exe和ABC.exe。
    用Append和Add差不多。
      

  3.   

    先谢谢你了,帮我解答两个问题了,但这个问题我试了好像不行啊。
    我干脆把我的目的和源代码让你看看吧,帮我改一下,谢谢了,有点乱!
    不好意思!
    目的是想找出字符串中所有扩展名为.exe的文件名,并把它们保存下来,但这段代码总是有重复的文件名,不知是哪里错了,请帮我修改一下。谢谢!(在线等待)
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      MyStrList:=TStringList.Create;
      str:='abcsafdj=abc.exe  abckli=xyz.exe kjl;jl=abc.exe';
      while Pos('.exe',str)>0 do
      begin
        p:=Pos('.exe',str)-1;
        s:='.exe';
        mychar:=copy(str,p+5,1);
        if (mychar='"') or (mychar=' ') or (mychar='>') then
        begin
          while (p>0) and not (Str[p] in ['/','\','=','"']) do
          begin
            s:=Str[p]+s;
            Dec(p);
          end;
            MyStrList.Duplicates := dupIgnore;//这条好像没起作用。
            MyStrList.Add(S);
            Delete(Str,1,p+5);
        end
        else
          delete(str,1,p+5);
      end;
      for i := 0 to myStrList.Count - 1 do
          MyStrList[i] := ChangeFileExt(MyStrList[i], '[A].exe');
          memo1.Lines:=mystrlist;//最后显示在MEMO里,很多重复的
    end;
      

  4.   

    Note: Duplicates does nothing if the list is not sorted.
    你需要添加:
      SL.Sorted := True;
      

  5.   

    属性Duplicates和Sorted最好在创建后立即设置。
      

  6.   

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      MyStrList:=TStringList.Create;
      MyStrList.Duplicates := dupIgnore;//这条好像没起作用。     
      str:='abcsafdj=abc.exe  abckli=xyz.exe kjl;jl=abc.exe';
      while Pos('.exe',str)>0 do
      begin
        p:=Pos('.exe',str)-1;
        s:='.exe';
        mychar:=copy(str,p+5,1);
        if (mychar='"') or (mychar=' ') or (mychar='>') then
        begin
          while (p>0) and not (Str[p] in ['/','\','=','"']) do
          begin
            s:=Str[p]+s;
            Dec(p);
          end;
            MyStrList.Add(S);
            Delete(Str,1,p+5);
        end
        else
          delete(str,1,p+5);
      end;
      for i := 0 to myStrList.Count - 1 do
          MyStrList[i] := ChangeFileExt(MyStrList[i], '[A].exe');
          memo1.Lines:=mystrlist;//最后显示在MEMO里,很多重复的
    end;
     試試吧!
      

  7.   

    可能是我疏忽了,需要将Sorted改为True。
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      MyStrList:=TStringList.Create;
      MyStrList.Duplicates := dupIgnore;
      MyStrList.Sorted := True;
      str:='abcsafdj=abc.exe  abckli=xyz.exe kjl;jl=abc.exe';
      while Pos('.exe',str)>0 do
      begin
        p:=Pos('.exe',str)-1;
        s:='.exe';
        mychar:=copy(str,p+5,1);
        if (mychar='"') or (mychar=' ') or (mychar='>') then
        begin
          while (p>0) and not (Str[p] in ['/','\','=','"']) do
          begin
            s:=Str[p]+s;
            Dec(p);
          end;
            MyStrList.Add(S);
            Delete(Str,1,p+5);
        end
        else
          delete(str,1,p+5);
      end;
      for i := 0 to myStrList.Count - 1 do
          MyStrList[i] := ChangeFileExt(MyStrList[i], '[A].exe');
          memo1.Lines:=mystrlist;//最后显示在MEMO里,很多重复的
    end;
      

  8.   

    改好了,这下我测试过了,可以了。你试试吧:procedure TForm1.Button1Click(Sender: TObject);
    var
      MyStrList: TStringList;
      str, s, myChar: string;
      p: Integer;
      i: INteger;
    begin
      MyStrList := TStringList.Create;
      MyStrList.Duplicates := dupIgnore;
      MyStrList.Sorted := True;
      str := 'abcsafdj=abc.exe  abckli=xyz.exe kjl;jl=abc.exe';
      while Pos('.exe', str) > 0 do
      begin
        p := Pos('.exe', str) - 1;
        s := '.exe';
        mychar := copy(str, p + 5, 1);
        if (mychar = '"') or (mychar = ' ') or (mychar = '>') then
        begin
          while (p > 0) and not (Str[p] in ['/', '\', '=', '"']) do
          begin
            s := Str[p] + s;
            Dec(p);
          end;
          MyStrList.Add(S);
          Delete(Str, 1, p + 5);
        end
        else
          delete(str, 1, p + 5);
      end;
      MyStrList.Sorted := False;
      for i := 0 to myStrList.Count - 1 do
        MyStrList[i] := ChangeFileExt(MyStrList[i], '[A].exe');
      memo1.Lines := mystrlist; //最后显示在MEMO里,很多重复的
    end;
      

  9.   

    程序运行后,一点这个按钮,DELPHI6就在闪着报警,还有一个错误对话框,
    标题栏是debugger exception notification
    下面还有很多内容:
    project1.exe raised exception class EStringListerror with message
    'Operation not allowed on sorted list'……
    什么意思?怎么解决?
      

  10.   

    试过了,我上面那个字符串是可以,但我换了一个网页源文件来试,就不行了,
    不仅很多重名的显示出来了,而且有很多名字都不全,比如abc.exe
    会出现c.exe这种情况,实际上没有c.exe这个文件名。