Delete all English letters in a string

I need to remove all English letters in a string.

I wrote the following code:

StringBuilder str = new StringBuilder();
foreach(var letter in test)
{   
    if(letter >= 'a' && letter <= 'z')
        continue;
    str.Append(letter); }

What is the fastest way?

+3
source share
3 answers

use the Regex replacement method and give it [az] | [AZ]

+2
source

Try the following:

var str = test.Where(item => item < 'A' || item > 'z' || (item > 'Z' && item < 'a'));
+1
source

Use this method to perform this execution ....

public static string RemoveSpecialCharacters (string str)

{

    StringBuilder sb = new StringBuilder();

    foreach (char c in str)

    {

        if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))

            continue;

        else

            sb.Append(c);


    }
    return sb.ToString();
}
0
source

All Articles