How to strip out numeric characters only from a string




I have a string of “Please bill 28kg” or sometimes “Bill 28lbs” or many more potential type of string that I would receive.

So now the question is how do I just strip out the numeric characters only from the string.

String - "Bill 28kg"
Expecting outcome - “28

Do anyone have any idea how to do this? I’ve tried Regex, but not sure how to do it as the output is in list of array. :frowning:



try this

string b= System.Text.RegularExpressions.Regex.Replace(“string_Variable”,"\D","")

It will replace all the characters other than digits and return only digits.



You’re the start Mahesh :slight_smile:


Hi Mahesh,

The code is really a good one. Unfortunately if the string contain “Please bill 28.4 kg”. It returns as “284”. Meaning to say it also strip out the decimal points. Can anycome please assist me on this?



I got it Mahesh, I’ve used [A-z]+ and it works :slight_smile:


I have similar case. I want to extract just the amount.
However my case is the amount appears: Balance Due CHF2,335.00.

I need to take care of the comma and decimal.

So using above the decimal , comma changes into *, the output appear as *233500



Try like this

string b = System.Text.RegularExpressions.Regex.Replace(“string_Variable”,"[^/./,0-9]","")



Great!! Thanks a lot!!