Runes and grapheme clusters

In Dart, runes expose the Unicode code points of a string.As of Dart 2.6, use the characters packageto view or manipulate user-perceived characters,also known asUnicode (extended) grapheme clusters.

Unicode defines a unique numeric value for each letter, digit,and symbol used in all of the world’s writing systems.Because a Dart string is a sequence of UTF-16 code units,expressing Unicode code points within a string requiresspecial syntax.The usual way to express a Unicode code point is\uXXXX, where XXXX is a 4-digit hexadecimal value.For example, the heart character (♥) is \u2665.To specify more or less than 4 hex digits,place the value in curly brackets.For example, the laughing emoji (😆) is \u{1f600}.

If you need to read or write individual Unicode characters,use the characters getter defined on Stringby the characters package.The returned Characters object is the string asa sequence of grapheme clusters.Here’s an example of using the characters API:

  1. import 'package:characters/characters.dart';
  2. ...
  3. var hi = 'Hi 🇩🇰';
  4. print(hi);
  5. print('The end of the string: ${hi.substring(hi.length - 1)}');
  6. print('The last character: ${hi.characters.last}\n');

The output, depending on your environment, looks something like this:

  1. $ dart bin/main.dart
  2. Hi 🇩🇰
  3. The end of the string: ???
  4. The last character: 🇩🇰

For details on using the characters package to manipulate strings,see the example and API referencefor the characters package.