string - 在 Dart 中分割表情符号字符串

标签 string flutter dart emoji

我想将一串表情符号拆分为每个表情符号。那么我怎样才能用 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/

相关文章:

c++ - 将 char 指针值等同于某个 char 时出现段错误

c - 如何使用 C 结构中的字符串字段?

flutter - Flutter:onEdittingComplete和onSubmitted之间的TextField差异

dart - 与容器装饰一起使用时,墨水瓶不显示波纹

firebase - Flutterfire ios 配置问题

flutter - 将子窗口小部件定位在“列”父窗口小部件的中心和末尾

c - 如何将字符串存储在数组中?

java - 如何在没有资源图像的情况下显示图像

java - Flutter WebView 在 html select 上崩溃

flutter - 使用Flutter从Firebase Firestore查询中获取单个文档