在Dart中拆分表情符号字符串

huangapple go评论110阅读模式
英文:

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('&#129325;&#127921;&#127955;')); // 输出: [&#129325;, &#127921;, &#127955;]
英文:

You can match all the emojis using regex, and then add them to a list:

List&lt;String&gt; splitEmoji(String text) {
  final List&lt;String&gt; out = [];
  final pattern = RegExp(
      r&#39;(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])&#39;);

  final matches = pattern.allMatches(text);
  for (final match in matches) {
    out.add(match.group(0)!);
  }
  return out;
}

Regex credit

Usage:

print(splitEmoji(&#39;&#129325;&#127921;&#127955;&#39;)); // Output: [&#129325;, &#127921;, &#127955;]

答案3

得分: 1

你可以使用 Stringrunes 属性。

void main() {
  final String emojis = '&#39;&#129325;&#127921;&#127955;&#39;';
  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 = &#39;&#129325;&#127921;&#127955;&#39;;
  final Runes codePoints = emojis.runes;
  for (final codePoint in codePoints) {
    print(String.fromCharCode(codePoint));
  }
}

huangapple
  • 本文由 发表于 2023年1月9日 00:32:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定