这几个函数都包含在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使用
拆分字符串的函数 [2005-12-13] 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;
|
请发表评论