Removing numbers from a string

I would like to know how I can remove numbers from String. I am trying to use StringReplace, and I do not know how to tell the function that I want to replace.

Here is what I tried:

StringReplace(mString, [0..9], '', [rfReplaceAll, rfIgnoreCase]); 
+7
source share
6 answers

Simple but effective. It can be optimized, but you should get what you need at the beginning:

 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; 
+11
source

Pretty quick version in place.

 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; 
+5
source

This is the same result as the Nick version, but it is more than 3 times faster with short lines. The longer the text, the greater the difference.

 function RemoveNumbers2(const aString: string): string; var C:Char; Index:Integer; begin Result := ''; SetLength(Result, Length(aString)); Index := 1; for C in aString do if not CharInSet(C, ['0' .. '9']) then begin Result[Index] := C; Inc(Index); end; SetLength(Result, Index-1); end; 

Do not waste precious processor cycles if you do not need it.

+5
source

Well, I'm tired of looking for already built functions, so I created my own:

  function RemoveNumbers(const AValue: string): string; var iCar : Integer; mBuffer : string; begin mBuffer := AValue; for iCar := Length(mBuffer) downto 1 do begin if (mBuffer[iCar] in ['0'..'9']) then Delete(mBuffer,iCar,1); end; Result := mBuffer; end; 
+1
source

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

0
source

StringReplace does not accept the set as the second argument. Maybe someone will have a more suitable approach, but this works:

 StringReplace(mString, '0', '', [rfReplaceAll, rfIgnoreCase]); StringReplace(mString, '1', '', [rfReplaceAll, rfIgnoreCase]); StringReplace(mString, '2', '', [rfReplaceAll, rfIgnoreCase]); 

and etc.

-one
source

All Articles