Runes in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

We know that strings in Dart are a sequence of Unicode UTF-16 characters. Dart Runes are actually UTF-32 Unicode code points.

They are UTF-32 string which is used to print special symbols.

For example, the theta symbol in dart is displayed when we assign the Unicode equivalent value of '\u0398' to a variable.

Example

Consider the example shown below −

 Live Demo

void main(){
   var heartSymbol = '\u0398';
   print(heartSymbol);
}

Output

Θ

There are different methods/properties that we can apply on dart Runes to extract the string core units. These mainly are −

  • string.codeUnitAt()

  • string.codeUnits

  • string.runes

string.codeUnitAt()

The string.codeUnitAt() method is used to access the character's code unit that is present inside the string. It takes one argument, the position(index) we want to get the code unit of.

Example

Consider the example shown below −

 Live Demo

void main(){
   String name = "tutorialspoint";
   print(name.codeUnitAt(0));
}

In the above code, we are printing the character unit at the 0th index of the string named name.

Output

116

string.codeUnits property

The string.codeUnits property is used to print the character units of each character in a string.

Example

Consider the example shown below −

 Live Demo

void main(){
   String name = "tutorialspoint";
   print(name.codeUnits);
}

Output

[116, 117, 116, 111, 114, 105, 97, 108, 115, 112, 111, 105, 110,
116]

string.runes property

The string.runes property of a string class is used to iterate the given string through the UTF-16 code unit.

Example

Consider the example shown below −

 Live Demo

void main(){
   String name = "tutorialspoint";
   name.runes.forEach((int rune){
      print(rune);
   });
}

Output

116
117
116
111
114
105
97
108
115
112
111
105
110
116
raja
Published on 24-May-2021 11:52:26
Advertisements