struntils单元的几个字符串处理方法
1.dupestring方法
var
s: string;
begin
s := 'Delphi';
s := DupeString(s,3);
ShowMessage(s); //DelphiDelphiDelphi
end;
2.trim,trimlelf,trimright方法
3.rightstr,leftstr方法
var
ss,s: string;
begin
ss := 'CodeGear Delphi 2007';
s := RightStr(ss,4);
ShowMessage(s); {2007}
s := LeftStr(ss,4);
ShowMessage(s); {Code}
end;
4.reversestring方法
var
ss,s: string;
begin
ss := 'Delphi';
s := ReverseString(ss);
ShowMessage(s); {ihpleD}
end;
5.quotedstr方法
var
ss,s: string;
begin
ss := 'Delphi';
s := QuotedStr(ss);
ShowMessage(s); {'Delphi'}
end;
6.comparestr,comparetext方法
var
s1,s2: string;
i: Integer;
begin
s1 := 'abc';
s2 := 'adc';
i := CompareStr(s1,s2);
ShowMessage(IntToStr(i)); {-2}
i := CompareText(s1,s2);
ShowMessage(IntToStr(i)); {-2}
s1 := 'abc';
s2 := 'ABC';
i := CompareStr(s1,s2); {CompareStr 区分大小写}
ShowMessage(IntToStr(i)); {32}
i := CompareText(s1,s2); {CompareText 不区分大小写}
ShowMessage(IntToStr(i)); {0}
end;
7.wraptext换行
var
ss,s: string;
begin
ss := 'aa.bb e-ff';
s := WrapText(ss, #13#10, ['.',' ',#9,'-'], 0);
ShowMessage(s); {aa.}
{bb }
{e- }
{ff }
s := WrapText(ss, #13#10, ['.',' ',#9,'-'], 5);
ShowMessage(s); {aa. }
{bb e- }
{ff }
s := WrapText(ss, #13#10, ['.',' ',#9,'-'], 10);
ShowMessage(s); {aa.bb e-ff}
end;
8.uppercase,lowercase方法
var
ss,s: string;
begin
ss := 'Delphi';
s := UpperCase(ss);
ShowMessage(s); //DELPHI
s := LowerCase(ss);
ShowMessage(s); //delphi
end;
9.stringreplace方法
var
ss,s: string;
begin
ss := '2007-2008';
s := StringReplace(ss,'00','x',[rfReplaceAll]);
ShowMessage(s); //2x7-2x8
s := StringReplace(ss,'00','x',[rfIgnoreCase]);
ShowMessage(s); //2x7-2008
end;
10.IsDelimiter - 判断字符串的某个位置是不是指定的字符串
var
s: string;
b: Boolean;
begin
s := 'CodeDear Delphi';
b := IsDelimiter('de', s, 3);
ShowMessage(BoolToStr(b)); {返回 -1, 就是 True}
end;
11.LastDelimiter - 判断一个字符串在另一个字符串中最后出现的位置
var
s: string;
i: Integer;
begin
s := 'CodeDear Delphi';
i := LastDelimiter('Del',s);
ShowMessage(IntToStr(i)); {12}
end;
12.format方法
var
s: string;
begin
s := 'del%s';
s := format(s,['phi']); {delphi}
end;