我想将一串表情符号拆分为每个表情符号。那么我怎样才能用 dart 语言做到这一点呢?
void main() {
print('GoodJob'.split("")); // output: [G, o, o, d, J, o, b]
print('🤭🎱🏓'.split("")); // output: [�, �, �, �, �, �] but expected: ['🤭','🎱','🏓']
}
最佳答案
来自TextField的文档建议使用characters在 dart 中使用表情符号的包。
文档描述如下,
It's important to always use characters when dealing with user input text that may contain complex characters. This will ensure that extended grapheme clusters and surrogate pairs are treated as single characters, as they appear to the user.
For example, when finding the length of some user input, use string.characters.length. Do NOT use string.length or even string.runes.length. For the complex character "👨👩👦", this appears to the user as a single character, and string.characters.length intuitively returns 1. On the other hand, string.length returns 8, and string.runes.length returns 5!
import 'package:characters/characters.dart';
void main() {
print('🤭🎱🏓'.characters.split("".characters));
}
输出
(🤭, 🎱, 🏓)
关于string - 在 Dart 中分割表情符号字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75049492/