How do you remove all the alphabetic characters from a string?

This should work:

// add directive at the top 
using System.Text.RegularExpressions;

string numberOnly = Regex.Replace(s, "[^0-9.]", "")

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(<yourString>, "[A-Za-z ]", "");
double parsedValue = double.Parse(value);

Assuming you have only alphabetic characters and space as units.


Using LINQ:

using System.Linq;

string input ="57.20000 KG ";
string output = new string(input.Where(c=>(Char.IsDigit(c)||c=='.'||c==',')).ToArray());

Tags:

C#

.Net