Delphi中用来截取字符的函数,以及使用方法?

2024-12-28 20:14:28
推荐回答(2个)
回答1:

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使用

回答2:

substring:=copy(sourcestring,iStartIndex,length);
example:
s:='hello world!';
s1:=copy(s,8,6);