When to use utf8 vs utf16 vs ascii character codes?

If I want to compare characters in iOS using the character code, how do I decide whether to compare the characters by their utf8 or utf16 or ascii code values?

I specifically would like to remove all characters from a CNPhoneNumber.stringValue that other than digits.

If you're doing this for a user-entered string, don't worry about how they're encoded.

CNPhoneNumber.stringValue is of type String

A String is a collection of Characters

You could iterate through the String by hand, but it is awkward. Swift offers compactMap, which will give you an array of single-character strings. A new String can be created from this Array.

for example

let likeAPhoneNumber = "(451)∕234-141😃0"
let newNumberArray = likeAPhoneNumber.compactMap { $0.isNumber ? $0 : nil }
let newNumber = String(newNumberArray)
print (newNumber)prints 4512341410

None of this is high-performance, because iterating through a Unicode string is non-trivial.

When to use utf8 vs utf16 vs ascii character codes?
 
 
Q