Skip to content

Instantly share code, notes, and snippets.

@musicm122
Created September 9, 2011 12:51
Show Gist options
  • Save musicm122/1206121 to your computer and use it in GitHub Desktop.
Save musicm122/1206121 to your computer and use it in GitHub Desktop.
Efficient Replace Special char method
public static string RemoveSpecialCharacters(string str)
{
int idx = 0;
char[] chars = new char[str.Length];
foreach (char c in str)
{
if ((c >= '0' && c <= '9')
|| (c >= 'A' && c <= 'Z')
|| (c >= 'a' && c <= 'z')
|| (c == '.') || (c == '_'))
{
chars[idx++] = c;
}
}
return new string(chars, 0, idx);
}