Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

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.

like image 522
Ashish Ashu Avatar asked Sep 02 '10 06:09

Ashish Ashu


People also ask

How do I remove the alphabet from a string in Python?

Python Remove Character from String using replace() We can use string replace() function to replace a character with a new character. If we provide an empty string as the second argument, then the character will get removed from the string.

How do you remove all of a character from a string?

Use the replace Function to Remove a Character From String in Java. The replace function can be used to remove a particular character from a string in Java. The replace function takes two parameters, the first parameter is the character to be removed, and the second parameter is the empty string.

How do I remove all characters from a string in Python?

Python Remove Character from a String – How to Delete Characters from Strings. In Python you can use the replace() and translate() methods to specify which characters you want to remove from a string and return a new modified string result.


2 Answers

This should work:

// add directive at the top  using System.Text.RegularExpressions;  string numberOnly = Regex.Replace(s, "[^0-9.]", "") 
like image 68
Pranay Rana Avatar answered Oct 14 '22 08:10

Pranay Rana


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.

like image 39
froeschli Avatar answered Oct 14 '22 08:10

froeschli