比如我有这个个字符串
 acceptstring:='[email protected],[email protected],[email protected],[email protected]'
我想以“,”号为分隔,把所有邮件地址放在一个数组里。代码应该怎么写。

解决方案 »

  1.   

    用copy,和delete  拷贝一段就删除一段,再把COPY得来的string添加到组件中即可
      

  2.   

    var
      StringList: TStringList;
      acceptstring: string;
    begin
      acceptstring:='[email protected],[email protected],[email protected],[email protected]';
      StringList := TStringList.Create;
      StringList.CommaText := acceptstring; 
      
      // 你的代码 
      Caption := StringList.Strings[0];  <---- //[email protected]  StringList.Free
    end;
      

  3.   

    怎么知道StringList数组的个数。
      

  4.   

    给你一个通用的算法,什么符号都行的:)procedure SeparateTerms(s : string;Separator : char;Terms : TStringList);
    { This browses a string and divide it into terms whenever the given
      separator is found. The separators will be removed }
      var
      hs : string;
      p : integer;begin
      Terms.Clear; // First remove all remaining terms
      if Length(s)=0 then   // Nothin' to separate
        Exit;
      p:=Pos(Separator,s);
      while P<>0 do
      begin
        hs:=Copy(s,1,p-1);   // Copy term
        Terms.Add(hs);       // Add to list
        Delete(s,1,p);       // Remove term and separator
        p:=Pos(Separator,s); // Search next separator
      end;
      if Length(s)>0 then
        Terms.Add(s);        // Add remaining term
    end;==========
    = 用  法 
    ==========var
    Terms : TStringList;
    i : integer;const
    TestStr = '1st term;2nd term;3rd term';//您要处理的字符串begin
      Terms:=TStringList.Create;
      SeparateTerms(TestStr,';',Terms);//分解,以分号来分,你改为逗号就行了
      for i:=0 to terms.Count-1 do
        ShowMessage(Terms.Strings[i]);
      Terms.Free;
    end;