比如字符串:
aaa;bbb  ccc;ddd,ef;kk
用什么方法将其分割到TStringList中,如下:
aaa
bbb ccc
ddd,ef
kk高手请指教,急啊

解决方案 »

  1.   

    用Pos、copy逐一截取即可实现;
      

  2.   

    var
     sL :TStringList;
      str,strtemp :string;
      i:integer;
    begin
      sL :=TStringList.Create;
      str :='sfadf;hhg;oaa;sds;dfdgd';
      try
        for i:=1 to Length(Str) do
        begin
          if Str[i]=';' then
          begin
            sL.Add(strtemp);
            strtemp :='';
          end
          else begin
            strtemp :=strtemp+str[i];
          end;
        end;
      except  end;
    end;
      

  3.   

    我想利用TStringList的CommText属性分割,但是这个属性不管是空格还是逗号都会分割,能指定其只按分号分割字符串吗?
      

  4.   

    使用GetToken函数function GetToken( var S: string;
      const Delimiters: TCharSet = [','] ) : string;
    var
      l, i, p: integer;
    begin
      result := '';
      if (S = '') then Exit;
      l:=Length(S);
      i:=1;
      p:=0;
      while (i<=l) and (p=0) do
      begin
        if S[i] in Delimiters
          then p:=i;
        Inc(i);
      end;
      if (p=0) and (i>l)
        then p:=i;
      if p>0 then
      begin
        SetLength(Result, p-1);
        Result:= Copy(S, 1, p-1);
        S:= Copy (S, p+1, l-p);
      end;
    end;该函数返回第一个由用户定义的分隔符位置前面的所有内容(不包含分隔符)。
    同时删除字符串中相应的部分(包括分隔符)。
      

  5.   

    看看StringList的Delimiter和DelimitedText属性(忘了是否准确,懒得开Delphi帮助了)的帮助
      

  6.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
        tem: string;
        len: integer;
        i: integer;
        j: integer;
        ss: string;
        ResArry: array[1..100] of integer;
    begin
        tem := '1 2 12 35 45 5 8 15';
        ss := '';
        len := length(tem);
        j := 1;
        for i := 0 to len do
        begin
            if copy(tem, 1, 1) = ';' then
            begin
                tem := copy(tem, 2, length(tem));
                ResArry[j] := strtoint(ss);
                j := j + 1;
                ss := '';
            end
            else
            begin
                ss := ss + (copy(tem, 1, 1));
                tem := copy(tem, 2, length(tem));
            end;        if i = len then
                resarry[j] := strtoint(ss);
        end;
        for i := 1 to j do
            showmessage(inttostr(ResArry[i]));
    end;我试了可以的,有什么问题发消息给我!!!!1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!