现有字符串
str:='56.27.25.63|||57.28.63.55|||21.75.122.533|||'
希望随机生成一个ip地址,但不能包含str中已有的地址。求这样的代码

解决方案 »

  1.   

    首先通过
    var
      ss:TStrings;
    begin
      ss:=TStringList.Create;
      ss.clear;
      extractstrings(['|||'],[],pchar(str),ss)
    end;
    得出字符串中已有IP地址
    然后再随机生成IP地址IP
    i:=ss.index(ip);
    判断i是否等于-1。如果等于-1就说明没在str中
      

  2.   

    随机生成ip地址代码procedure TForm1.Button1Click(Sender: TObject);
    var
      ip,s,s1,s2,s3,s4:string;
      i,j:Integer;
      b:Boolean;
    begin
      b:=False;
      while b=False do
      begin
        Randomize;
        ip:='';
        for j:=1 to 12 do
        begin
          i:=Random(9);
          s:=IntToStr(i);
          if (j<>4) and (j<>7) and (j<>10) then
            ip:=ip+s
          else
            ip:=ip+'.'+s;
        end;
        if Copy(ip,1,3)='000' then
          b:=False
        else
          b:=True;
      end;
      s1:=Copy(ip,1,3);
      s2:=Copy(ip,5,3);
      s3:=Copy(ip,9,3);
      s4:=Copy(ip,13,3);
      ip:=IntToStr(StrToInt(s1))+'.'+IntToStr(StrToInt(s2))+'.'+IntToStr(StrToInt(s3))+'.'+
          IntToStr(StrToInt(s4));
      self.Label1.Caption:=ip;
    end;
      

  3.   

    请帮忙看一下这个代码:var strip:string;  
    var ss:string;Randomize;
    ss:='56.27.25.63|||57.28.63.55|||21.75.122.533|||';while ss.indexOf(strip)>-1 do     ////////////////////这里报错
    begin
     strip=Format('10.%d.%d.%d',
        [Random(255), Random(255), Random(254) + 1])
    end请问该怎么写?