I'd like to know how I can delete numbers from a String. I try to use StringReplace and I don't know how to tell the function that I want to replace numbers.
Here's what I tried:
StringReplace(mString, [0..9], '', [rfReplaceAll, rfIgnoreCase]);
Simple but effective. Can be optimized, but should get you what you need as a start:
function RemoveNumbers(const aString: string): string;
var
C: Char;
begin
Result := '';
for C in aString do begin
if not CharInSet(C, ['0'..'9']) then
begin
Result := Result + C;
end;
end;
end;
use this
function RemoveNonAlpha(srcStr : string) : string;
const
CHARS = ['0'..'9'];
var i : integer;
begin
result:='';
for i:=0 to length(srcStr) do
if (srcstr[i] in CHARS) then
result:=result+srcStr[i];
end ;
you can call it like this
edit2.text:=RemoveNonAlpha(edit1.text);
Pretty quick inplace version.
procedure RemoveDigits(var s: string);
var
i, j: Integer;
pc: PChar;
begin
j := 0;
pc := PChar(@s[1]);
for i := 0 to Length(s) - 1 do
if pc[i] in ['0'..'9'] then
//if CharInSet(pc[i], ['0'..'9']) for Unicode version
Inc(j)
else
pc[i - j] := pc[i];
SetLength(s, Length(s) - j);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With