How can I reduce an NSString to lowercase, ascii alpha characters (/a-z/)? Non-english alpha characters should be converted to ascii characters. Specifically, uppercase characters become lowercase (A → a), non-english characters become english characters (ñ → n), and all other characters are discarded.
For example: “Västerås, Sweden” becomes “vasterassweden”.
You can convert a string to an
NSDatacontaining bytes representing the string in a different encoding and allow for lossy conversion usingdataUsingEncoding:allowLossyConversion. Then you can just convert theNSDataback to anNSString:Result is “Vasteras, Sweden”. Now there is a
lowercaseStringmethod etc. if you need to go further.Edit:
Oops, missed the end of the question “and all other characters are discarded”, you do wish to go further:
That looks horrible (and a good argument to convert to a C-string and do it more directly), but it does the job:
componentsSeparatedByCharactersInSet:returns an array of strings by treating the characters in the supplied set as the separators;componentsJoinedByString:joins an array of strings into a single string separated by the supplied string – and we’ve passed an empty joiner.