Open In App

Dart – Runes

Last Updated : 12 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Dart language, strings are simply a sequence of UTF-16 (16-bit Unicode Transformation Format) code units. The Unicode format maps a unique numeric character to represent every digit, letter, or symbol. 

A rune can be defined as an integer used to represent any Unicode code point. As a Dart string is a simple sequence of UTF-16 code units, 32-bit Unicode values in a string are represented using a special syntax. The String class in the dart:core library gives ways to access runes. Runes can be accessed in the following ways :

  • Using String.codeUnits property
  • Using String.runes property
  • Using String.codeUnitAt() function

String.codeUnits property

This property returns an unchangeable list of the 16-bit UTF-16 code units of the given string.

Syntax:

String. codeUnits;

Example:

Dart




import 'dart:core';  
main(){ 
   String gfg = 'GeeksforGeeks'
   print(gfg.codeUnits); 
}


Output:

[71, 101, 101, 107, 115, 102, 111, 114, 71, 101, 101, 107, 115]

String.runes Property

String.runes extend Iterable. This property returns an iterable of Unicode code-points of the specified string.

Syntax:

String.runes;

Example:

Dart




main(){ 
   String gfg="GFG";
   gfg.runes.forEach((int x) { 
      var ch=new String.fromCharCode(x); 
      print(ch); 
   });  
}


Output:

G
F
G

String.codeUnitAt() Function

It is used to return the UTF-16 code unit at the specified index of this string.

Syntax:

String.codeUnitAt(int index);

Example:

Dart




import 'dart:core'
main(){ 
   String gfg = 'GeeksforGeeks'
   print(gfg.codeUnitAt(2)); 


Output:

101


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads