delphi的Split函数 4个版本


本文整理自网络,侵删。

 delphi的Split函数 4个版本
一、直接使用如下函数(注:ch只能是单字符,如键盘上英文状态下的字符)

function SplitString(const Source,ch:String):TStringList;
var
temp:String;
i:Integer;
begin
Result:=TStringList.Create;
//如果是空自符串则返回空列表
if Source=''
then exit;
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;

二、直接使用TStringList

procedure TForm1.Button3Click(Sender: TObject);
var
Str:String;
ResultList:TStringList;
I:Integer;
begin
str:= '南京~信息~工程~大学';

ResultList := TStringList.Create;
try
ResultList.Delimiter := '~';
ResultList.DelimitedText := str;

for I:= 0 to ResultList.Count-1 do
begin
Memo1.Lines.Add(ResultList.Strings[I]);
end;
finally
FreeAndNil(ResultList);
end;
end;

三、支持特殊字符版(ch可以为字符串,如'aa')

function SplitString(const Source,ch:String):TStringList;
var
Temp:String;
I:Integer;
chLength:Integer;
begin
Result:=TStringList.Create;
//如果是空自符串则返回空列表
if Source='' then Exit;
Temp:=Source;
I:=Pos(ch,Source);
chLength := Length(ch);
while I<>0 do
begin
Result.Add(Copy(Temp,0,I-chLength+1));
Delete(Temp,1,I-1 + chLength);
I:=pos(ch,Temp);
end;
Result.add(Temp);
end;

 

 

四、算是改进版 支持 如:===== 或  ---- 

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;

相关阅读 >>

Delphi 控制台读写

Delphi 一个call应该如何写?

Delphi sysutils.lastdelimiter - 判断一个字符串在另一个字符串中最后出现的位置

Delphi中使用xmlhttp / xmlhttprequest 避免缓存

Delphi createmutex建立互斥对象,并且给互斥对象起一个唯一的名字

Delphi xe android]获取屏幕的物理分辨率

Delphi winapi: getwindowtextlength - 获取窗口标题长度

Delphi 字符串中末位是双字节字符的处理(避免最后一位为乱码)

Delphi判断字符串是否是汉字

Delphi 测试字符串写入类: tstringwriter

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...