function StringReplace(const S, OldPattern, NewPattern: string; Flags: TReplaceFlags): string;TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);rfReplaceAll Replace all occurrences. If this flag is not present, only the first occurrence of the target substring is replaced. rfIgnoreCase Match occurrences of the substring case-insensitively. If this flag is not present, only case-sensitive matches are considered.
var str:string; begin str:='we have we have we have'; str:=StringReplace(str, 'we', 'rr',[rfIgnoreCase]);//将第一个'we'替换为'rr' str:=StringReplace(str, 'we', 'rr',[rfReplaceAll]);//将所有的'we'替换为'rr'
rfIgnoreCase Match occurrences of the substring case-insensitively. If this flag is not present, only case-sensitive matches are considered.
begin
str := StringReplace(str, ' ', '', [rfReplaceAll]); //去空格
str := StringReplace(str, #7, '', [rfReplaceAll]);
str := StringReplace(str, #13#10, '', [rfReplaceAll]);
Result:=str;
end;
str:string;
begin
str:='we have we have we have';
str:=StringReplace(str, 'we', 'rr',[rfIgnoreCase]);//将第一个'we'替换为'rr'
str:=StringReplace(str, 'we', 'rr',[rfReplaceAll]);//将所有的'we'替换为'rr'
[] 替换第一个,区分大小写
[rfReplaceAll]替换全部,区分大小写
[rfIgnoreCase]替换第一个,不区分大小写
[rfReplaceAll,rfIgnoreCase]替换全部,不区分大小写
StringReplace('AaaaAaa','a','b',[rfReplaceAll]) = 'AbbbAbb'
StringReplace('AaaaAaa','a','b',[rfIgnoreCase]) = 'baaaAaa'
StringReplace('AaaaAaa','a','b',[rfReplaceAll,rfIgnoreCase]) = 'bbbbbbb'