delphi的字符截取函数LeftStr,MidStr,RightStr的介绍以及字符串拆分  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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;
推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.046756029129028 seconds