1 year ago
#198149
Aurora1510
Swift Convert Int to String.Index with and without Emojis to get part of String
I am trying to send a string to an API, which corrects me the text. But the problem is, that text with emoji fails.
I have the following string: "Hello hwo are you?"
Then I send this string to an API, which corrects me the string. So it returns the following information: ["replace": "how", offset: 6, length: 3].
Now I send the string with emoji: "Hello 😋 Hwo are you?". So it returns the following information: ["replace: "How", offset: 9, length: 3].
The thing is that the API gives me the information that the length of the emoji is 2 characters.
My code on how to convert it works fine without the emoji:
correctText = oldText.prefix(offset) + replaceText + oldText.dropFirst(offset + length)
But with the emoji this fails due to one index. How do I fix this problem?
swift
string
nsstring
0 Answers
Your Answer