有一字符串 如:STR1:='1.02  aaa bb     c    ddd bm' 中间用空格分开,但空格的数目不相同,
如,1.02和 AAA 中间可能有3个,而BB与AAA中只有一个. 求一简单的算法,将以上
数据分开逐个添加到TStringList的变量list 中
    50分给一人,留下的其它人分(先给出方法的50分),通过者立马给分.
   

解决方案 »

  1.   


    var
      str1:string;
      list:TStringList;
      i:integer;
    begin
      str1:='1.02  aaa bb    c    ddd bm' ;
      list := TStringList.Create;
      list.DelimitedText := str1;
      memo1.Lines.AddStrings(list);
      list.Free;
    end;
      

  2.   


    var
      s: String;
      List: TStringList;
    begin
      s := '1.02  aaa bb    c    ddd bm' ;
      List := TStringList.Create;
      ExtractStrings([' '],[' '],PChar(s),List);
      Showmessage(List[4]);
      List.Free;
    end;
      

  3.   

    http://blog.sina.com.cn/s/blog_40ac71670100anjg.html
      

  4.   

    用状态机,详见 Delphi 算法与数据结构一书!我写过的,现在手边没代码,明天粘给你!
      

  5.   


    Uses
      IdStrings;
    var
      s: String;
      List: TStringList;
    begin
      s := '1.02  aaa bb    c    ddd bm' ;
      List := TStringList.Create;
      SplitColumns(s,List,' ');
      List.Free;
    end;
      

  6.   

    Function TCountWord.ExtractWords(const S: string): TStringDynArray;
    type
      TStates = (ScanNormal,ScanQuoted,ScanPunctuation);
    const
      WordDelim = ' !<>[](){},./?;~:-+=*@#$%^&"12345567890';
    var
      State: TStates;
      Inx: integer;
      Ch: Char;
      CurWord: string;  procedure AddToList(Const aWord: string);
      var
        aLen: integer;
      begin
        aLen := Length(Result);
        SetLength(Result,aLen+1);
        Result[aLen] := lowerCase(aWord);
      end;
    begin
      SetLength(Result,0);
      State := ScanNormal;
      CurWord :='';
      //读入串中所有字符
      for Inx := 1 to Length(S) do
      begin
          //获取下一个字符
          Ch := S[Inx];
          //对于状分情况处理
          case State of
              ScanNormal:
              begin
                  if Pos(Ch,WordDelim)<>0 then
                  begin
                      if (CurWord<>'') then
                      begin
                          AddToList(CurWord);
                          CurWord := '';
                      end;
                      state := ScanPunctuation;
                  end else
                  begin
                      CurWord := CurWord+Ch;
                  end;
              end;
              ScanQuoted:
              begin
                  CurWord := CurWord+Ch;
                  State := ScanNormal;
              end;
              ScanPunctuation:
              begin
                  if Pos(Ch,WordDelim) = 0 then
                  begin
                      CurWord := Ch;
                      State := ScanNormal;
                  end;
              end;      end;
      end;
      if CurWord<>'' then
        AddToList(CurWord);
    end;