现在的位置: 首页 > 综合 > 正文

Delphi的Split函数

2013年03月07日 ⁄ 综合 ⁄ 共 416字 ⁄ 字号 评论关闭

因为Delphi中并没有自带的分拆字符串的函数,但实际编程中又经常会用到。在网上找了一段代码,在使用中发现有Bug,没有考虑到分拆符可能不只一个字符的情况。下面是修改过后的代码(红色部分是修改的代码):

function SplitStr(const Source, Splitter: String):TStringList;

var

  temp: String;

  i: Integer;

begin

  Result := TStringList.Create;

  //如果是空自符串则返回空列表

  if Source='' then

    Exit;

  temp := Source;

  i := pos(Splitter, Source);

  while i <> 0 do

  begin

     Result.add(copy(temp, 0, i-1));

     Delete(temp, 1, i - 1 + Length(Splitter));

     i:=pos(Splitter, temp);

  end;

  Result.add(temp);

end;

抱歉!评论已关闭.