Most efficient way to remove special characters from string
Posted
by ObiWanKenobi
on Stack Overflow
See other posts from Stack Overflow
or by ObiWanKenobi
Published on 2009-07-13T15:33:42Z
Indexed on
2010/03/27
19:33 UTC
Read the original article
Hit count: 221
c#
|string-manipulation
I want to remove all special characters from a string. Allowed characters are A-Z (uppercase or lowercase), numbers (0-9), underscore (_), or the dot sign (.).
I have the following, it works but I suspect (I know!) it's not very efficient:
public static string RemoveSpecialCharacters(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if ((str[i] >= '0' && str[i] <= '9') || (str[i] >= 'A' && str[i] <= 'z' || (str[i] == '.' || str[i] == '_')))
sb.Append(str[i]);
}
return sb.ToString();
}
What is the most efficient way to do this? What would a regular expression look like, and how does it compare with normal string manipulation?
The strings that will be cleaned will be rather short, usually between 10 and 30 characters in length.
© Stack Overflow or respective owner