例如:有这样一个字符串111|BPS|222|BPS|333|BPS|444|BPS|555
我希望能依次从这个字符串取出111,222,333,444,555。
不好意思,很菜的一个问题,我之前写了一个,可老是要少取几个,所以还请各位大侠帮忙????

解决方案 »

  1.   

    Delphi好象有一个根据标志取字符串的函数d大概就是XXX(str,'|'),忘啦!
    找到再说
      

  2.   

    type
      TResultArray = array of string;function SplitString(const source, ch: string): TResultArray;
    var
      temp: string;
      i: integer;
    begin
      temp := source;
      i := pos(ch, source);
      while i <> 0 do
      begin
        SetLength(Result, Length(Result) + 1);
        Result[Length(Result) - 1] := copy(temp, 0, i - 1);
        delete(temp, 1, i);
        i := pos(ch, temp);
      end;
      SetLength(Result, Length(Result) + 1);
      Result[Length(Result)-1] := Temp;
    end;
    **************
    function SplitString(const source,ch:string):tstringlist;
    var
     temp:string;
     i:integer;
    begin
     result:=tstringlist.Create;
     temp:=source;
     i:=pos(ch,source);
     while i<>0 do
     begin
       result.Add(copy(temp,0,i-1));
       delete(temp,1,i);
       i:=pos(ch,temp);
     end;
     result.Add(temp);
    end;
    调用:
    s:=splitstring('afsdfsdaaa|bbfdsfsdb|ccc','|');
    for i:=0 to s.Count-1 do
     b:=b+s.Strings[i]+#13;
    showmessage(b);
    s.free;
      

  3.   

    放到一个TListBox中吧:
    var
      TmpStr,SrcStr:String;
    begin
      SrcStr:='111|BPS|222|BPS|333|BPS|444|BPS|555'
      ListBox1.Items.Clear;
      while Pos('|',SrcStr)>0 do
      begin
        TmpStr:=Copy(SrcStr,1,Pos('1',SrcStr)-1);
        Delete(StrStr,1,Pos('1',SrcStr));
        if TmpStr<>'' then  ListBox1.Items.Add(TmpStr); 
      end;
      if SrcStr<>'' then
        ListBox1.Items.Add(StrStr);
    end;
    //随手写的,可能有些不对的地方,给你参照一下吧
      

  4.   

    分割字符串函数
    Function TForm1.CutString(Str:String):String;
    var
    N:integer;
    Tmp:String;
    begin
    Tmp:='';
    N:=pos('|BPS|',Str);
    While (N>0) do
    begin
    Tmp:=tmp+copy(Str,N-3,3)+',';
    str:=copy(str,N+5,length(str));
    N:=pos('|BPS|',Str);
    end;
    Result:=Tmp+Str;
    end;//调用函数
    procedure TForm1.Button1Click(Sender: TObject);
    var
    S:string;
    begin
    S:='111|BPS|222|BPS|333|BPS|444|BPS|555';
    label1.Caption:=CutString(S);
    end;
      

  5.   

    推荐上面的 cutstring() 
     
    其实实现这种功能的算法不少,自己仔细琢磨琢磨。编一个很容易的!
      

  6.   

    你可以把类似|BPS|的字符串命成一个变量如 str1调用  cutstring(str,str1);在过程中N:=pos(str1,str);这样过程更实用
      

  7.   

    我给你写一个通用性的。//s:分割符;  str :字符串
    function TForm1.MyPos(s, str: string): TstringList;
    var
      i,j: Integer;
      trmSList: TstringList;
    begin
      trmSList := TStringList.Create;
      trmSList.Add('');
      j := 0;
      for i := 1 to Length(str) do
        if s = str[i] then
        begin
          trmSList.Add('');
          j := j + 1;
        end
        else
          trmSList.Strings[j] := trmSList.Strings[j] + str[i];  Result := trmSList;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowMessage(Self.MyPos(Edit1.Text,Edit2.Text).Text);
    end;