Dart Programming Tutorial on Dart Programming Runes

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]

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

runes