Trimming/replacing all instances of a character in a string
Sometimes you want to replace or remove all instances of a character in a string. Say you have '000123' and you want '123'. Call this function like this: tmpStr := TrimAllChar('000123', '0'); You can replace the '' with any character you want. function TrimAllChar(const S: string; const ch: Char): string;
{Removes/replaces all occurences of a character from a string}
var
buf: string;
begin
buf := S;
Result := '';
{while Pos finds a blank} while (Pos(ch, buf) > 0) do begin {copy the substrings before the blank in to Result}
Result := Result + Copy(buf, 1, Pos(ch, buf) - 1);
buf := Copy(buf, Pos(ch, buf) + 1, Length(buf) - Pos(ch, buf));
end;
{There will still be a remainder in buf, so copy remainder into Result}
Result := Result + buf;
end;