ePrivacy and GPDR Cookie Consent by CookieConsent.com
menu

Runes in dart Language

First, you should know What Unicode is, Unicode is used for defining all the letters, number, symbols, characters of the world in a numeric value. It is like a roll number for every letter, digits, etc.

Second, You should now what UTF-16 is, UTF-8 is an encoding used to translate numbers into binary data.

Dart strings are UTF-16 encoded

Runes are also Strings but having UTF-32 encoding. But dart uses UTF-16 encoding for its strings. So to display runes dart uses a special syntax that is

\uXXXX
here \u refers Unicode value and XXXX is a 4-digit hexadecimal value. 


Sometimes we have more than four hexadecimal values then we move hex digits into curly braces.

\u{XXXXXX}
e.g. \u{3d943} 


For example 

Dart
void main(){
   
   var heart = '\u2665';
    print(heart); 
}

Output:  ?

There are three ways to access runes in dart which are provided by String class of dart: core library.

  1. String.codeUnitAt() function
  2. String.codeUnits() property
  3. String.runes property

String.codeUnitAt() function

This function is used for accessing the code units of a String on a given Index. Tt returns  16-bit UTF-16 code units.

Syntax

String.codeUnitAt(int index);


Example

Dart
void main(){
   
   var str = 'This is a string';
    print(str.codeUnitAt(0)); 
}
Output: 84


 

String.codeUnits property

This function returns code units of all the characters of a string into a list.

Syntax

String.codeUnits;


Example

Dart
void main(){
   
   var str = 'This is a string';
    print(str.codeUnits); 
}
Output: [84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103]


 

String.runes property

This property returns an iterable of Unicode code-points of this String.runes extend iterable.

Syntax

String.runes


Example

Dart
void main(){ 
    var x = "This is a string";
    x.runes.forEach((int rune){		            // Iterating each runes
      var letter = String.fromCharCode(rune);    // Converting codeUnits into letters
      print(letter);		                    // printing final letters
    });
} 

Comments