I don't understand what's going on here:
let strA = "0123456789"
let fromA = String.Index(encodedOffset: 2)
let toA = String.Index(encodedOffset: 8)
print("\(String(strA[fromA..<toA]))") // Prints "234567".
let strB = "01235689"
let fromB = String.Index(encodedOffset: 2)
let toB = String.Index(encodedOffset: 8)
print("\(String(strB[fromB..<toB]))") // Prints "23".
I would expect the bottom line to print "2356"
but it prints "23"
. This seems to be an issue to do with unicode code points and partially chopping off part of the last unicode emoji character.
How can I get my expected "2356"
string using the offsets of the visible characters?
My issue stems from Twitter's API and its extended tweets JSON objects that require you to chop the tweet text down to size using display_text_range
values. Right now my code crashes whenever there is an emoji in the tweet as my substring code, as above, corrupts the string.