开发者

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开发者_如何学C >= 'a' && letter <= 'z')
        continue;
    str.Append(letter); }

What is the fastest way?


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


Try this:

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


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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜