In Swift, how do I check whether the first character of a String
is ASCII or a letter with an accent? Wider characters, like emoticons & logograms, don't count.
I'm trying to copy how the iPhone Messages app shows a person's initials beside their message in a group chat. But, it doesn't include an initial if it's an emoticon or a Chinese character for example.
I see decomposableCharacterSet
& nonBaseCharacterSet
, but I'm not sure if those are what I want.
There are many Unicode Character with an accent. Is this for you a character with an accent?
Ê Ð Ï Ḝ Ṹ é⃝
In Unicode there are combined characters, which are two unicode characters as one:
For Swift the
Character
is the same!Good reference is here.
If you want to know the CodeUnit of the Characters in the String, you can use the
utf8
orutf16
property. They are different!Then you could check for ASCII alphabet A-Z as 65-90 and a-z as 97-122. And then check for the standard accent grave and acute À 192 Á 193 È 200 É 201 à 224 á 225 è 232 é 233 ... and the combined ones and everything you like.
But there are symbols that look like a latin letter with accent, but doesn't have the same meaning!
You should make sure, that only these characters are accepted, that you want with the correct linguistic meaning.