Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only letters from a string in C#?

Tags:

string

c#

I have a string and need the letters from said string.

string s = "EMA123_33";    // I need "EMA" string s = "EMADRR123_33"; // I need "EMADRR" 

I am using C# in Visual Studio 2008.

like image 999
user745607 Avatar asked Sep 06 '11 07:09

user745607


People also ask

How do I get only letters from string?

Extract alphabets from a string using regex You can use the regular expression 'r[^a-zA-Z]' to match with non-alphabet characters in the string and replace them with an empty string using the re. sub() function. The resulting string will contain only letters.

How do you find a specific letter in a string C?

The strchr() function finds the first occurrence of a character in a string. The character c can be the null character (\0); the ending null character of string is included in the search.

How do you check if a string contains only alphabets and numbers in C?

C isalpha() The isalpha() function checks whether a character is an alphabet or not. In C programming, isalpha() function checks whether a character is an alphabet (a to z and A-Z) or not. If a character passed to isalpha() is an alphabet, it returns a non-zero integer, if not it returns 0.

How do I get only the letters of a string in C++?

bool my_predicate(char c); Then use the std::remove_if algorithm to remove the unwanted characters from the string: std::string s = "my data"; s.


1 Answers

You can try this:

var myString = "EMA123_33"; var onlyLetters = new String(myString.Where(Char.IsLetter).ToArray()); 

please note: this version will find "e" just like "E" - if you need only upper-case letters then do something like this:

var myString = "EMA123_33"; var onlyLetters = new String(myString.Where(c => Char.IsLetter(c) && Char.IsUpper(c)).ToArray()); 
like image 106
Random Dev Avatar answered Sep 22 '22 00:09

Random Dev