英文:
Split emoji string in Dart
问题
我想将一串表情符号分割成每个表情符号。在Dart语言中,我该如何做?
void main() {
print('GoodJob'.split('')); // 输出: [G, o, o, d, J, o, b]
print('🤭🎱🏓'.split('&#')); // 输出: [, 129325;, 127921;, 127955;] 但期望的是: ['🤭', '🎱', '🏓']
}
英文:
I want to split a string of emojis into each emoji. so how can I do this in dart language?
void main() {
print('GoodJob'.split("")); // output: [G, o, o, d, J, o, b]
print('🤭🎱🏓'.split("")); // output: [�, �, �, �, �, �] but expected: ['🤭','🎱','🏓']
}
答案1
得分: 4
文档来自TextField,建议在Dart中使用characters包来处理表情符号。
文档描述如下,
> 在处理可能包含复杂字符的用户输入文本时,始终使用characters
是很重要的。这将确保扩展的形式集群和代理对被视为单个字符,就像它们对用户显示的那样。
> 例如,当查找一些用户输入的长度时,请使用string.characters.length
。不要使用string.length
,甚至不要使用string.runes.length
。对于复杂字符"👨👩👂",这对用户来说显示为单个字符,string.characters.length
直观地返回1。另一方面,string.length
返回8,string.runes.length
返回5!
import 'package:characters/characters.dart';
void main() {
print('👨👩👂'.characters.split(''.characters));
}
输出
(👨, 👩, 👂)
英文:
Docs from TextField recommends to use characters package to work with emoji in dart.
Docs describe as follows,
> 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));
}
outputs
(🤭, 🎱, 🏓)
答案2
得分: 1
你可以使用正则表达式匹配所有的表情符号,然后将它们添加到一个列表中:
List<String> splitEmoji(String text) {
final List<String> out = [];
final pattern = RegExp(
r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])');
final matches = pattern.allMatches(text);
for (final match in matches) {
out.add(match.group(0)!);
}
return out;
}
用法:
print(splitEmoji('🤭🎱🏓')); // 输出: [🤭, 🎱, 🏓]
英文:
You can match all the emojis using regex, and then add them to a list:
List<String> splitEmoji(String text) {
final List<String> out = [];
final pattern = RegExp(
r'(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])');
final matches = pattern.allMatches(text);
for (final match in matches) {
out.add(match.group(0)!);
}
return out;
}
Usage:
print(splitEmoji('🤭🎱🏓')); // Output: [🤭, 🎱, 🏓]
答案3
得分: 1
你可以使用 String
的 runes
属性。
void main() {
final String emojis = ''🤭🎱🏓'';
final Runes codePoints = emojis.runes;
for (final codePoint in codePoints) {
print(String.fromCharCode(codePoint));
}
}
英文:
You can use the runes
property of String
.
void main() {
final String emojis = '🤭🎱🏓';
final Runes codePoints = emojis.runes;
for (final codePoint in codePoints) {
print(String.fromCharCode(codePoint));
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论