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.
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 −
- Using String.codeUnitAt() function
- Using String.codeUnits property
- Using String.runes property
String.codeUnitAt() Function :
Code units in a string can be accessed through their indexes. Returns the 16-bit UTF-16 code unit at the given index.
Syntax
String.codeUnitAt(int index);
Example
import 'dart:core';
void main(){
f1();
}
f1() {
String x = 'Runes';
print(x.codeUnitAt(0));
}
Output:
82
String.codeUnits Property :
This property returns an unmodifiable list of the UTF-16 code units of the specified string.
Syntax:
String. codeUnits;
Example:
import 'dart:core';
void main(){
f1();
}
f1() {
String x = 'Runes';
print(x.codeUnits);
}
Output:
[82, 117, 110, 101, 115]
String.runes Property
This property returns an iterable of Unicode code-points of this string.Runes extends iterable.
Syntax
String.runes
Example:
void main(){
"A string".runes.forEach((int rune) {
var character=new String.fromCharCode(rune);
print(character);
});
}
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.