Search code examples
c#stringletter

Move all non-letter characters from one string to array of char


I have a string with letters and non-letter characters. I want to use non-letter characters in the String.Split() method to split the string into words divided by non-letter characters. How can I do this? I know, there's a Char.IsLetter() method, but I think it would be stupid to execute this method in a loop for every character of the string. I need to consider as letter all the characters of the English alphabet and the apostrophe ('), all other characters are not letters. Thanks.

P.S. IsPunctuation method wouldn't work, I need something with IsLetter method. Thanks again


Solution

  • LINQ is your friend here.

         var testString = "TEST.string;here";
         var nonChars = testString.Where(f => !char.IsLetter(f) && f != '\'').ToArray();
    

    Edited slightly to more closely match the spec