Bernardo Ferrari
08/07/2018, 2:36 AMval str = "ađcde"
val start = str.indexOf(str.first()) + 2
val end = str.indexOf(str.last()) - 2
val range = IntRange(start, end)
println(str.substring(range)) // "should output đc, but will output ?c"
Swift makes use of a Range<String.Index>, instead of Range<Int> (IntRange). The String Index is calculated based on a particular string so that it knows if there are any emoji or extended grapheme clusters. Don't you think would be nice to see a StringRange kind of thing on Kotlin?
Source: https://stackoverflow.com/a/35193481/4418073gildor
08/07/2018, 2:47 AMgildor
08/07/2018, 2:49 AMgildor
08/07/2018, 2:53 AMgildor
08/07/2018, 2:57 AMgildor
08/07/2018, 2:58 AMval str = "ađcde"
str.length // 6
str.codePoints().count() // 5
Bernardo Ferrari
08/07/2018, 3:04 AMgildor
08/07/2018, 3:07 AMgildor
08/07/2018, 3:07 AMgildor
08/07/2018, 3:08 AMgildor
08/07/2018, 3:08 AMgildor
08/07/2018, 3:12 AMgildor
08/07/2018, 3:12 AMgildor
08/07/2018, 3:18 AMval str = "ađcde"
str.codePointCount(0, str.length) // 5
Bernardo Ferrari
08/07/2018, 3:21 AMBernardo Ferrari
08/07/2018, 3:21 AMgildor
08/07/2018, 3:25 AMI wanted the user to type a single âglyphâ, letter or emojiItâs actually not so simple problem. For example some emojis is combination of multiple code points, for example emoji Family is combination of multiple code points, that rendered by font renderer in a specific way
Bernardo Ferrari
08/07/2018, 3:28 AMgildor
08/07/2018, 3:31 AMcalculating the paragraph number based on a range of text, and generating regex tokensI donât see why do you need to know how emojis rendered by UI to solve those problems
Bernardo Ferrari
08/07/2018, 3:36 AMdamian
08/08/2018, 4:16 AMBreakIterator
will probably accomplish what you are looking for - BreakIterator.getCharacterInstance
will return an instance that allows you to loop over and count the number of graphemes