...replace occurences of a substring in a string?
Author: Thomas Stutz
{
Description:
StringReplace replaces occurrences of the substring specified
by OldPattern with the substring specified by NewPattern.
Beschreibung:
Mit StringReplace ersetzen Sie in dem angegebenen String den im
Parameter OldPattern festgelegten Sub-String mit dem im Parameter
NewPattern übergebenen neuen Sub-String.
type
TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function StringReplace(const S, OldPattern, NewPattern: string;
Flags: TReplaceFlags): string;
- rfReplaceAll :
If the Flags parameter does not include rfReplaceAll, StringReplace only
replaces the first occurrence of OldPattern in S. Otherwise, all instances
of OldPattern are replaced by NewPattern.
Wenn im Parameter Flags nicht rfReplaceAll enthalten ist, wird nur das erste
Vorkommen von OldPattern im String S ersetzt.
Andernfalls werden alle Instanzen von OldPattern durch NewPattern ersetzt.
- rfIgnoreCase :
If the Flags parameter includes rfIgnoreCase,
The comparison operation is case insensitive.
Wenn der Parameter Flags rfIgnoreCase enthält,
wird die Groß-/Kleinschreibung nicht berücksichtigt.
}
procedure TForm1.Button1Click(Sender: TObject);
var
sNewText: string;
begin
// Replace the first Word "FOX" with "tiger".
// Ersetzt das erste Wort "FOX" durch "tiger".
sNewText := StringReplace('The quick brown fox jumps over the lazy fox.',
'FOX', 'tiger', [rfIgnoreCase]);
ShowMessage(sNewText);
// Remove all Spaces in a string.
// Alle Leerzeichen in einem String ersetzen.
sNewText := StringReplace('This is a Text with Spaces', ' ', '', [rfReplaceAll]);
ShowMessage(sNewText);
// Replace all "True" in a Memo with "False".
// Alle Wörter "True" mit "False" ersetzen in einem Memo.
Memo1.Text := StringReplace(Memo1.Text, 'True', 'False', [rfReplaceAll, rfIgnoreCase]]);
end;
{******************************************************}
{2.}
{
StringReplace is not available for D3/D2/D1 users.
Use the slightly modified StringReplace below.
(from sysutils.pas but without ANSI support)
}
type
TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase);
function StringReplace(const S, OldPattern, NewPattern: string;
Flags: TReplaceFlags): string;
var
SearchStr, Patt, NewStr: string;
Offset: Integer;
begin
if rfIgnoreCase in Flags then
begin
SearchStr := UpperCase(S);
Patt := UpperCase(OldPattern);
end
else
begin
SearchStr := S;
Patt := OldPattern;
end;
NewStr := S;
Result := '';
while SearchStr <> '' do
begin
Offset := Pos(Patt, SearchStr);
if Offset = 0 then
begin
Result := Result + NewStr;
Break;
end;
Result := Result + Copy(NewStr, 1, Offset - 1) + NewPattern;
NewStr := Copy(NewStr, Offset + Length(OldPattern), MaxInt);
if not (rfReplaceAll in Flags) then
begin
Result := Result + NewStr;
Break;
end;
SearchStr := Copy(SearchStr, Offset + Length(Patt), MaxInt);
end;
end;
{******************************************************}
{3.}
{
In S alle OldSubStr mit NewSubStr ersetzen.
Replace all OldSubStr in S with NewSubStr.
}
function ReplaceStr(s, OldSubStr, NewSubStr: string): string;
var
i: integer;
OldSubLen, TotalLength: integer;
begin
Result := '';
if s <> '' then
begin
OldSubLen := Length(OldSubStr); // für die Performance - for performance
TotalLength := Length(s);
i := 1;
while i <= TotalLength do
begin
if (i <= TotalLength - OldSubLen) and (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;
printed from
www.swissdelphicenter.ch
developers knowledge base