Irirna
Irirna

Reputation: 33

Delete all english letters in string

I need to delete 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?

Upvotes: 3

Views: 664

Answers (3)

Akram Shahda
Akram Shahda

Reputation: 14781

Try this:

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

Upvotes: 1

AB Vyas
AB Vyas

Reputation: 2389

Use this method to do such 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();
}

Upvotes: 0

Chen Kinnrot
Chen Kinnrot

Reputation: 21015

use Regex replace method, and give it [a-z]|[A-Z]

Upvotes: 2

Related Questions