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:
import 'package:characters/characters.dart';
...
var hi = 'Hi 🇩🇰';
print(hi);
print('The end of the string: ${hi.substring(hi.length - 1)}');
print('The last character: ${hi.characters.last}\n');
The output, depending on your environment, looks something like this:
$ dart bin/main.dart
Hi 🇩🇰
The end of the string: ???
The last character: 🇩🇰
For details on using the characters package to manipulate strings,see the example and API referencefor the characters package.