一串字符,想逐个提取中括号里的字符

解决方案 »

  1.   

    一个while循环,然后判断Pos('[',字符串)大于0吗,如果大于0就取[和]之间的值,然后把取出值的串删了
      

  2.   


    uses StrUtils;procedure TForm1.FormCreate(Sender: TObject);
    var
      I: integer;
      S: string;
    begin
      S := '[1][2][3][4][5]';  with TStringList.Create do
      begin
        Delimiter := ']';
        DelimitedText := AnsiReplaceText(S, '[', '');
        for I:=0 to Count-1 do
          if Strings[I]<>'' then ShowMessage(Strings[I]);
        Free;
      end;
    end;
      

  3.   


    procedure TForm1.Button1Click(Sender: TObject);
    var
      s,str: string;
      i,l: integer;
      b: boolean;
    begin
      b:= false;
      s:= '日期:[fDate] 单据号:[fNo]';
      l:= length(s);
      for i:= 1 to l do
      begin
      if not b then
        if s[1] <> '[' then delete(s,1,1) else
          begin
            b:= true;
            delete(s,1,1);
          end
      else
      begin
        if s[1] <> ']' then str:= str+s[1] else
          begin
            b:= false;
            Memo1.Lines.Append(str);
            str:= '';
          end;
        delete(s,1,1);
      end
      end;
      showmessage(str);
    end;
      

  4.   

    function AnalyzeStr(const S:string;AList:TStrings):Integer;
    var
      i,j:Integer;
    begin
      i:=Pos('[',S);
      while i>0 do
      begin
        j:=PosEx(']',S,i);
        if (i>0)and(j>0) then
          AList.Add(Copy(S,i+1,j-i-1))
        else if j<=0 then
          break;
        i:=PosEx('[',S,j);
      end;
      Result:=AList.Count;
    end;procedure TForm1.btn1Click(Sender: TObject);
    var
      AList:TStringList;
      S:String;
    begin
      s:= '日期:[fDate] 单据号:[fNo]';
      AList:=TStringList.Create;
      AnalyzeStr(S,AList);
      Showmessage(AList.Text);
      AList.Free;
    end;