How to strip out numeric characters only from a string

activities
studio

#1

Hi,

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.

Example:
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:


#2

@Serran_Neru

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.

Regards,
Mahesh


#3

You’re the start Mahesh :slight_smile:


#4

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?

Regards,
Serran


#5

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


#6

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


#7

@Mallika

Try like this

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

Regards,
Mahesh


#8

Great!! Thanks a lot!!