Dart Runes and Graphemes
Runes and Graphemes data types in Dart
Runes and Graphemes
Runes are the special string of Unicode UTF-32 bits that represent the special syntax. Unicode defines a unique numeric value for each letter, digit, and symbol.
The Dart Runes are provided by the ‘ dart : core ’ library or ‘ package : characters ’.
Syntax of representing runes is :
\uXXXX, where XXXX is a 4-digit hexadecimal value
For example,
\u2665 corresponds to a heart character.
Program
import 'package:characters/characters.dart' ;
void main( )
{
print( ' \u2665 ' ) ;
print( ' \u{ 1f606 } ' ) ;
}
Output:
The String code runes can be accessed using the following three methods :
- Using String.codeUnitAt( ) function
- Using String.codeUnits property
- Using String.runes property
String.codeUnitAt( ) Function
This function returns the 16 – bit UTF – 16 code unit corresponding to the string at a particular index passed to the function as an argument.
Implementation = String.codeUnitAt( int index ) ;
Program
void main( )
{
// initializing string str
String str = ' Let us understand codeUnits Property ' ;
// using codeUnitAt function to return the code unit at index 0
print( str.codeUnitAt( 0 ) ) ;
// using codeUnitAt function to return the code unit at index 2
print( str.codeUnitAt( 2 ) ) ;
}
Output :
32
101
String.codeUnits Property
This property returns the list of the UTF – 16 code units of the specified string.
Implementation = String. codeUnits ;
Program
void main( )
{
// initializing string str
String str = ' This tutorial teaches Runes ' ;
print( ' \n The code units are as follows : \n ' );
// Using codeUnits property to print the code units of each string
print( str.codeUnits ) ;
}
Output :
The code units are as follows :
[32, 84, 104, 105, 115, 32, 116, 117, 116, 111, 114, 105, 97, 108, 32, 116, 101, 97, 99, 104, 101, 115, 32, 82, 117, 110, 101, 115, 32]
String.runes Property
This property returns an iterable of Unicode code – points of the passed string.Runes.
Implementation = String.runes
Program
void main( )
{
" This teaches Runes ".runes.forEach( ( int rune ){
var character = new String.fromCharCode( rune ) ;
print( character ) ;
} ) ;
}
Output:
T
h
i
s
t
e
a
c
h
e
s
R
u
n
e
s