delphi的字符截取函数LeftStr,MidStr,RightStr的介绍以及字符串拆分


本文整理自网络,侵删。

 这几个函数都包含在StrUtils中,所以需要uses StrUtils; 
假设字符串是 Dstr := 'Delphi is the BEST', 那么
LeftStr(Dstr, 5) := 'Delph'
MidStr(Dstr, 6,7) := 'i is th'
RightStr(Dstr, 6) := 'e BEST'

~~~~~~~~~~~~~~~~~~~~~~~~~
function RightStr
(Const Str: String; Size: Word): String;
begin
if Size > Length(Str) then Size := Length(Str);
RightStr := Copy(Str, Length(Str)-Size+1, Size)
end;
function MidStr
(Const Str: String; From, Size: Word):String;
begin
MidStr := Copy(Str, From, Size)
end;
function LeftStr
(Const Str: String; Size: Word): String;
begin
LeftStr := Copy(Str, 1, Size)
end;

这几个函数经常结合Pos, Length, Copy使用


拆分字符串的函数

delphi中没有提供此类函数,从大富翁找了一个

function split(src,dec : string):TStringList;
var
i : integer;
str : string;
begin
result := TStringList.Create;
repeat
i := pos(dec,src);
str := copy(src,1,i-1);
if (str='') and (i>0) then
begin
delete(src,1,length(dec));
continue;
end;
if i>0 then
begin
result.Add(str);
delete(src,1,i+length(dec)-1);
end;
until i<=0;
if src<>'' then
result.Add(src);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
ss : TStringList;
str,dec : string;
begin
str := '1111||2222||||3333|||4444||';
dec := '||';
ss := split(str,dec);
memo1.Lines.AddStrings(ss);
ss.Free;
end;

相关阅读 >>

Delphi windows 根据进程名结束进程的代码

Delphi 实现数据库连接封装到dll

Delphi 程序启动窗体控制在桌面右下角

Delphi 的内存操作函数(3): 给结构体指针分配内存

Delphi判断ip地址是否正确

Delphi 关闭进程方法

Delphi idhttp访问datasnap有密码验证的中间件

Delphi调用android振动功能

Delphi中如何捕获mediaplayer播放的视频中的每一帧 ?

Delphi thread.queue与synchronize的区别

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



打赏

取消

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

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

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

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

评论

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