tags:

views:

60

answers:

3

I have a string that containg alaphabets for example :

a) 254.69 meter b) 26.56 cm c) 23.36 inches d) 100.85 ft

I want to remove all the alphabets (units) from the above mentiones strings so that I can call double.Parse method !!

Please help!!

+3  A: 
Regex.Replace(s, "[^0-9.]", "")
Pranay Rana
Probably want to preserve any sign: `"[^0-9.+-]"`
BillP3rd
@BillP3rd - but wants to remove alphabates and string is of units with number. do you really think its required ?
Pranay Rana
Thanks Pranay for the solution. BillP3rd , at present i don't need +- signs , but thanks for the update, may be in future I do want to preserve the signs.
Ashish Ashu
A: 

You should be able to solve this using Regex. Add the following reference to your project:

using System.Text.RegularExpressions;

after that you can use the following:

string value = Regex.Replace(, "[A-Za-z ]", ""); double parsedValue = double.Parse(value);

Assuming you have only alphabetic characters and space as units.

froeschli
+1  A: 

Use CharMatcher API from Google's Guava library:

String magnitudeWithUnit = "254.69 meter"; String magnitude = CharMatcher.inRange('a', 'z').or(inRange('A', 'Z')).removeFrom(magnitudeWithUnit);

Do static-import CharMatcher.inRange(..). You can trim the results for trailing space.

nabeelalimemon