-
Notifications
You must be signed in to change notification settings - Fork 1.7k
substring does not support UTF-16 #35798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
CC @lrhn |
Here's my workaround for now: String.fromCharCode(str.runes.first) |
A substring method that works on code points rather than UTF-16 code units might be inefficient. Eg, taking a substring near the end of a very long string would be slow, because it would have to iterate through most of the string counting code points. The substring documentation doesn't make it clear that the method operates on code units. Can we update it to explain this, similarly to the good explanation given on the [] operator? |
I am having the exact same problem. My case is I want to programmatically delete (backspace) a text input which may have emojis. So far, my workaround is as follows: var s = "abc😀";
var sRunes = s.runes;
print(String.fromCharCodes(sRunes, 0, sRunes.length-1)); And make sure users do not input those emojis which have length 4. |
FYI, My another workaround https://stackoverflow.com/a/56135774/348719 which is currently broken. |
I have found another workaround , is more respource expensive but so far it works with all emojis. In my particular case i wanted to do a subtring [EX from index 0 to 16], and count an emoji as an individual character , however it was just getting half of the text due to the emojis in it My workaround is this one Create a function called runeSubtring() like this one
and just use it like this when you like
If the text is really big, you should do |
See #28404 and dart-lang/language#34, long discussions about making correct String manipulation easier. We will probably close this issue as being one example of the bigger issue. @LiteCatDev String runeSubstring({String input, int start, int end}) {
return String.fromCharCodes(input.runes.toList().sublist(start, end));
} |
this SO might be related https://stackoverflow.com/questions/54483177/handling-grapheme-clusters-in-dart |
dart-lang/language#685 is our current attempt at supporting this via a package; closing the present issue in favor of that |
Similar issue I need to solve have posted problem here please give your inputs thanks |
Use |
This issue came up because a user inputted an emoji into a data field. Attempting to render
Text("🍕".substring(0, 1))
in Flutter results in:The text was updated successfully, but these errors were encountered: