有个edit.text内容如edit1.text='13,3,13,5,18';那就是有13是重复的;如果里面有重复的数字就不执行,该怎么判断,我是通过分成为5个数字,我只能写得出,逐个比较的方法,不知道有什么好的判断方法吗

解决方案 »

  1.   

    1:把当前分隔符前后的字符串分为两个字符串变量. varA,varB
    2:if pos(varB,varA) =0  then 执行你想执行的代码 else  varA:=varB 
    3:一直重复 第 1,2步,直到没有分隔符.....
      

  2.   

    将子串传给stringlist,利用stringlist排序,然后按顺序判断
    或者你自己排序....
      

  3.   

    procedure TForm1.Button2Click(Sender: TObject);
    var
      str1,str2:string;
      i:integer;
      isrepeated:boolean;
    begin
      isrepeated:=false;
      str1:=Edit1.Text;
      for I := 0 to Length(str1) - 1 do
        begin
          if Pos(',',str1)<0 then Break;
           str2:=Copy(str1,Pos(',',str1)+1,Length(str1)-1);
          str1:=Copy(str1,0,Pos(',',str1)-1);
          if Pos(str1,str2)=0 then str1:=str2 else
            begin
             isrepeated:=true;
             Break;
            end;
        end;
     if isrepeated=False then ShowMessage('字符串内无重值') else ShowMessage('字符串内有重值')
    end;
      

  4.   


    function CheckDuplicate(S: String): Boolean;
    var
      AList: TStringList;
      i    : Integer;
    begin
      Result := False;
      AList := TStringList.Create;
      try
        AList.Duplicates := dupAccept;
        AList.Sorted := True;
        AList.Delimiter := ',';
        AList.DelimitedText := S;
        for i := 1 to AList.Count - 1 do
          if AList.Strings[i] = AList.Strings[i - 1] then
          begin
            Result := True;
            break;
          end;
      finally
        AList.Free;
      end;
    end;//调用的例子
      if CheckDuplicate('13,3,13,5,18') then ShowMessage('数据有重复');
      

  5.   

    直接打上来的,未在IDE下测试...fuction GetStrList(aText : string): TStringList;
    begin
      Result := nil;
      try
        Result := TStringList.Create;
        Result.Text := aText;
        Result.Sort;
      except
        on e: exception do
          showmessage('GetStrList Failed:'+ e.message);
      end;
    end;function HaveDuplicateValue(const aStr: string): Boolean;
    var
      sLst : TStringList;
      iCount: Integer;
    begin
      Result:= False;
      Try
        vLst := GetStrList(aStr);
        For iCount := 0 to vLst.Count-2 do
        begin
          
          if vLst.Strings[iCount]=vLst.Strings[iCount+1] then
          begin
            Result := True;
            //你可以在这里回传重复值
            Break;
          end;
        end;
      Finally
        vLst.Free;
      End;
    end;
      

  6.   

    我觉得我的方法处理起来,不一定会比jadeluo的慢.....