skip to Main Content

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: ['🤭','🎱','🏓']
}

3

Answers


  1. 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;
    }
    
    

    Regex credit

    Usage:

    print(splitEmoji('🤭🎱🏓')); // Output: [🤭, 🎱, 🏓]
    
    Login or Signup to reply.
  2. 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

    (🤭, 🎱, 🏓)
    
    Login or Signup to reply.
  3. 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));
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search