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

C#.Net

C# Problem Overview


I have a string containg alphabetic characters, for example:

  1. 254.69 meters
  2. 26.56 cm
  3. 23.36 inches
  4. 100.85 ft

I want to remove all the alphabetic characters (units) from the above mentioned strings so that I can call the double.Parse() method.

C# Solutions


Solution 1 - C#

This should work:

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

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

Solution 2 - C#

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.

Solution 3 - C#

Using LINQ:

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

Solution 4 - C#

The Regex, and Vlad's LINQ answers cover the solution well. And are both good options.

I had a similar problem, but I also wanted to only explicitly strip letters, and not to strip white space or etc, with this variant.

I also wanted it usable as below. Any of the other solutions could be packaged in a similar manner.

public static string StripAlpha(this string self)
{
    return new string( self.Where(c => !Char.IsLetter(c)).ToArray() );
}

public static string StripNonNumeric(this string self)
{
    // Use Vlad's LINQ or the Regex Example
    return new string(self.Where(c=>(Char.IsDigit(c)||c=='.'||c==',')).ToArray()) ;  // See Vlad's

}

This would then be used such as:

var newString = someString.StripAlpha();
var newString2 = someString.StripNonNumeric();

Solution 5 - C#

Hi another solution is:

// add directive at the top 
using System.Text.RegularExpressions;
string s = "24,99";
string numberOnly = Regex.Replace(s, "[^0-9,-]+", "")

This solution will not remove the dot e.g. question from user1804084:

this remove the dot for me e.g. 24.99 somechracter -> 2499

However it can still be convertet to a double where adding and subtraction works as normal.

Solution 6 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshish AshuView Question on Stackoverflow
Solution 1 - C#Pranay RanaView Answer on Stackoverflow
Solution 2 - C#froeschliView Answer on Stackoverflow
Solution 3 - C#Vlad S.View Answer on Stackoverflow
Solution 4 - C#GregView Answer on Stackoverflow
Solution 5 - C#SteffenView Answer on Stackoverflow
Solution 6 - C#user18943View Answer on Stackoverflow