I am working on application to show mobile contact list with initials in a circle, but not getting initial character for some contact names.
In below code, first name is from mobile’s contact list and second one I have typed from keyboard.
I am able to get correct length and also first character of the second name, but length for first name is double and also not able to get first character (it gives �).
print("𝙽𝚊𝚐𝚎𝚜𝚑".substring(0,1)); //�
print("𝙽𝚊𝚐𝚎𝚜𝚑".length); //12
print("Nagesh".substring(0,1)); //N
print("Nagesh".length); //6
Thankyou in advance for answering….
2
Answers
The strings look similar, but they consist of different unicode characters.
Character
U+1d67d
"𝙽"
is not the same asU+004e
"N"
.You can use
str.runes.length
to get the number of unicode characters.A detailed explanation why the string length is different can be found here
example:
You can use this function to use substring with unicode:
and you can use it like this:
you can use this function instead of default
substring
.