Removing all non letter characters from a string in C#

J

jack3604

Guest
I want to remove all non letter characters from a string. When I say all letters I mean anything that isn't in the alphabet, or an apostrophe. This is the code I have.

public static string RemoveBadChars(string word)
{
char[] chars = new char[word.Length];
for (int i = 0; i < word.Length; i++)
{
char c = word;

if ((int)c >= 65 && (int)c <= 90)
{
chars = c;
}
else if ((int)c >= 97 && (int)c <= 122)
{
chars = c;
}
else if ((int)c == 44)
{
chars = c;
}
}

word = new string(chars);

return word;
}


It's close, but doesn't quite work. The problem is this:

[in]: "(the"
[out]: " the"


It gives me a space there instead of the "(". I want to remove the character entirely.

Continue reading...
 
Top