jeudi 9 avril 2015

Referencing Alphabetic characters in C# string

I have a string that where I am trying to remove any characters that are not alphabetic. In the foreach loop below, I am wondering if there is a better way to reference the alphabetic characters without having to type each one of them out and without using the regex method.



foreach (char c in phrase)
{
if (!(c == 'a' || c == 'b' || c == 'c' ||c == 'd' || c == 'e' || c == 'f' || c == 'g' || c == 'h' || c == 'i' ||
c == 'j' ||c == 'k' ||c == 'l' ||c == 'm' ||c == 'n' ||c == 'o' ||c == 'p' ||c == 'q' ||c == 'r' ||c == 's' ||
c == 't' ||c == 'u' ||c == 'v' ||c == 'w' ||c == 'x' ||c == 'y' ||c == 'z'))
{
int i = phrase.IndexOf(c);
phrase = phrase.Remove(i, 1);
}
}


Having it written out that way seems sloppy and it can be time consuming. Any Suggestions? Thanks


Aucun commentaire:

Enregistrer un commentaire