Gallery
Software
Music
Programming
Archive
Friday, Jul 19 Week 29
type
TCharSet = set of ansiChar; //Allows multiple chars as argument in routine below

const
NULL = #0;

function removeChars(p:pAnsiChar;ch:TCharSet):ansiString;
var rP:pAnsiChar;k:longWord;
begin
rP:=p;k:=0; //Reset
while rP^NULL do begin //Find length and count chars
if rP^ in ch then inc(k); //Number of chars to remove
inc(rP); //Next char
end;
setLength(result,longWord(rP-p)-k); //Set result length
rP:=pChar(result); //Get result pointer
while p^NULL do begin //Fill data into result
if not (p^ in ch) then begin rP^:=p^;inc(rP) end; //Write char if not excluded
inc(p); //Next char
end;
end;

Example

procedure TForm1.FormCreate(Sender: TObject);
var s:ansiString;
begin
s:='1.2.3'; //Define string to delete chars from
s:=removeChars(pAnsiChar(s),['.']); //Remove DOTS from string "s"
caption:=string(s); //Show the result in form caption
end;

Related pages of Remove certain chars from a string

Delphi programming
Programming
Delphi examples
Character sets
Fast copy characters from a string
Count characters from a string