初次使用Delphi,我想请问怎么样分割字符串,比如有一字符串“aaa-bbb-ccc”,我怎么样把“aaa”,“bbb”,“ccc”取出来,有类似与VB中的split的函数,吗?

解决方案 »

  1.   

    delphi没有,自己写个吧
    function getfield(const sour: string; num: integer; sep: char='|'): string;
    var
        count, len, i, j: integer;
    begin
        result := '';
        len := length(sour);
        count := 0;
        if (num < 0) or (len <= num) then
            exit;
        for i := 1 to len do
        begin
            if count = num then
            begin
                j := i;
                while (sour[j] <> sep) and (j <= len) do
                begin
                    result := result + sour[j];
                    j := j + 1;
                    if j > len then
                        exit;
                end;
                exit;
            end
            else if sour[i] = sep then
                inc(count);
        end;
    end;
      

  2.   

    没有这样的函数,自己写一个吧
    function ExtractFirst(var Str:String):string;
    //Result为第一个
    //剩下的部分在str中
    var i:Integer;
    begin 
      i:=Pos('-',Str);
      if i=0 then 
      begin
        Result:=Str;
        Str='';
      end
      else
      begin
         Result:=Copy(str,1,i-1);
         str:=Copy(str,i+1,10000);//10000应该足够了
      end;end;