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();
}
精彩评论