2006. január 27., péntek

Remove extra spaces from a string


Problem/Question/Abstract:

I would like to strip all extra/ unnecessary spaces from a string. Meaning, if there are two or more space characters next to each other, I want to strip all but one. How can I do this?

Answer:

Solve 1:

{ ... }
st := 'This    is a   test';
p: pos('  ', st);
while p <> 0 do
begin
  delete(st, p, 1);
  p: pos('  ', st);
end;
{ ... }


Solve 2:

{ ... }
while pos('  ', st) > 0 do
  st := StringReplace(st, '  ', ' ', [rfReplaceAll]);
{ ... }

Nincsenek megjegyzések:

Megjegyzés küldése