Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete numbers from a String

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]);
like image 908
CharleyXIV Avatar asked May 31 '12 17:05

CharleyXIV


3 Answers

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;
like image 98
Nick Hodges Avatar answered Oct 15 '22 22:10

Nick Hodges


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);

like image 33
asdk2006 Avatar answered Oct 16 '22 00:10

asdk2006


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;
like image 29
MBo Avatar answered Oct 15 '22 22:10

MBo