Title: replace occurences of a substring in a string?
{
Description:
StringReplacereplacesoccurrencesofthesubstringspecified
byOldPatternwiththesubstringspecifiedbyNewPattern.
Beschreibung:
MitStringReplaceersetzenSieindemangegebenenStringdenim
ParameterOldPatternfestgelegtenSub-StringmitdemimParameter
NewPatternbergebenenneuenSub-String.
type
TReplaceFlags=setof(rfReplaceAll,rfIgnoreCase);
functionStringReplace(constS,OldPattern,NewPattern:string;
Flags:TReplaceFlags):string;
-rfReplaceAll:
IftheFlagsparameterdoesnotincluderfReplaceAll,StringReplaceonly
replacesthefirstoccurrenceofOldPatterninS.Otherwise,allinstances
ofOldPatternarereplacedbyNewPattern.
WennimParameterFlagsnichtrfReplaceAllenthaltenist,wirdnurdaserste
VorkommenvonOldPatternimStringSersetzt.
AndernfallswerdenalleInstanzenvonOldPatterndurchNewPatternersetzt.
-rfIgnoreCase:
IftheFlagsparameterincludesrfIgnoreCase,
Thecomparisonoperationiscaseinsensitive.
WennderParameterFlagsrfIgnoreCaseenthlt,
wirddieGro-/Kleinschreibungnichtbercksichtigt.
}
procedureTForm1.Button1Click(Sender:TObject);
var
sNewText:string;
begin
//ReplacethefirstWord"FOX"with"tiger".
//ErsetztdasersteWort"FOX"durch"tiger".
sNewText:=StringReplace('Thequickbrownfoxjumpsoverthelazyfox.',
'FOX','tiger',[rfIgnoreCase]);
ShowMessage(sNewText);
//RemoveallSpacesinastring.
//AlleLeerzeichenineinemStringersetzen.
sNewText:=StringReplace('ThisisaTextwithSpaces','','',[rfReplaceAll]);
ShowMessage(sNewText);
//Replaceall"True"inaMemowith"False".
//AlleWrter"True"mit"False"ersetzenineinemMemo.
Memo1.Text:=StringReplace(Memo1.Text,'True','False',[rfReplaceAll,rfIgnoreCase]]);
end;
{******************************************************}
{2.}
{
StringReplaceisnotavailableforD3/D2/D1users.
UsetheslightlymodifiedStringReplacebelow.
(fromsysutils.pasbutwithoutANSIsupport)
}
type
TReplaceFlags=setof(rfReplaceAll,rfIgnoreCase);
functionStringReplace(constS,OldPattern,NewPattern:string;
Flags:TReplaceFlags):string;
var
SearchStr,Patt,NewStr:string;
Offset:Integer;
begin
ifrfIgnoreCaseinFlagsthen
begin
SearchStr:=UpperCase(S);
Patt:=UpperCase(OldPattern);
end
else
begin
SearchStr:=S;
Patt:=OldPattern;
end;
NewStr:=S;
Result:='';
whileSearchStr''do
begin
Offset:=Pos(Patt,SearchStr);
ifOffset=0then
begin
Result:=Result+NewStr;
Break;
end;
Result:=Result+Copy(NewStr,1,Offset-1)+NewPattern;
NewStr:=Copy(NewStr,Offset+Length(OldPattern),MaxInt);
ifnot(rfReplaceAllinFlags)then
begin
Result:=Result+NewStr;
Break;
end;
SearchStr:=Copy(SearchStr,Offset+Length(Patt),MaxInt);
end;
end;
{******************************************************}
{3.}
{
InSalleOldSubStrmitNewSubStrersetzen.
ReplaceallOldSubStrinSwithNewSubStr.
}
functionReplaceStr(s,OldSubStr,NewSubStr:string):string;
var
i:integer;
OldSubLen,TotalLength:integer;
begin
Result:='';
ifs''then
begin
OldSubLen:=Length(OldSubStr);//frdiePerformance-forperformance
TotalLength:=Length(s);
i:=1;
whileido
begin
if(iand(Copy(s,i,OldSubLen)=OldSubStr)then
begin
Result:=Result+NewSubStr;
Inc(i,OldSubLen);
end
else
begin
Result:=Result+s[i];
Inc(i);
end;
end;
end;
end;