gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:47:00 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com