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 −
- 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));
}
It will produce the following 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);
}
It will produce the following output −
[82, 117, 110, 101, 115]
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
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);
});
}
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.
Example
main() {
Runes input = new Runes(' \u{1f605} ');
print(new String.fromCharCodes(input));
}
It will produce the following output −
Leave a Reply