Strings are a sequence of characters. Dart represents strings as a sequence of Unicode UTF-16 code units. Unicode is a format that defines a unique numeric value for each letter, digit, and symbol.
Since a Dart string is a sequence of UTF-16 code units, 32-bit Unicode values within a string are represented using a special syntax. A rune is an integer representing a Unicode code point.
The String class in the dart:core library provides mechanisms to access runes. String code units / runes can be accessed in three ways −
Code units in a string can be accessed through their indexes. Returns the 16-bit UTF-16 code unit at the given index.
String.codeUnitAt(int index);
import 'dart:core'; void main(){ f1(); } f1() { String x = 'Runes'; print(x.codeUnitAt(0)); }
It will produce the following output −
82
This property returns an unmodifiable list of the UTF-16 code units of the specified string.
String. codeUnits;
import 'dart:core'; void main(){ f1(); } f1() { String x = 'Runes'; print(x.codeUnits); }
It will produce the following output −
[82, 117, 110, 101, 115]
This property returns an iterable of Unicode code-points of this string.Runes extends iterable.
String.runes
void main(){ "A string".runes.forEach((int rune) { var character=new String.fromCharCode(rune); print(character); }); }
It will produce the following output −
A s t r i n g
Unicode code points are usually expressed as \uXXXX, where XXXX is a 4-digit hexadecimal value. To specify more or less than 4 hex digits, place the value in curly brackets. One can use the constructor of the Runes class in the dart:core library for the same.
main() { Runes input = new Runes(' \u{1f605} '); print(new String.fromCharCodes(input)); }
It will produce the following output −