If you need to replace multiple spaces (double spaces) in a string to single space with Delphi you can use next text function:
- function StringReplace (const SourceString, OldPattern, NewPattern : string; Flags : TReplaceFlags) : string;
The StringReplace function replaces the first or all occurences of a substring OldPattern in SourceString with NewPattern according to Flags settings. The changed string is returned.
The Flags may be none, one, or both of these set values:
- rfReplaceAll : Change all occurrences
- rfIgnoreCase : Ignore case when searching
These values are specified in square brackets.
To replace double spaces to single space using Delphi see example:
uses SysUtils;
var
before, after : string;
begin
before:='Some text with double spaces';
after := StringReplace(before, ' ', ' ', [rfReplaceAll]);
ShowMessage(before);
//Output: Some text with double spaces
ShowMessage(after);
//Output: Some text with double spaces
end;
To replace multiple spaces to single space using Delphi you can use next function:
function DelDoubleSpaces(OldText:String):string;
var i:integer;
s:string;
begin
if length(OldText)>0 then
s:=OldText[1]
else
s:='';
for i:=1 to length(OldText) do
begin
if OldText[i]=' ' then
begin
if not (OldText[i-1]=' ') then
s:=s+' ';
end
else
begin
s:=s+OldText[i];
end;
end;
DelDoubleSpaces:=s;
end;