Table of contents
1.
Introduction
2.
Runes In Dart
3.
Methods in Dart Runes
4.
Accessing Runes
4.1.
Using String.codeUnits Property
4.2.
Using String.runes Property
4.3.
Using String.codeUnitAt() Function
5.
Frequently Asked Questions
5.1.
What is the code unit for the dart string?
5.2.
In Dart, what tool is used to format code?
5.3.
How long are Dart strings actually?
5.4.
In Dart, what is the integer's default value?
6.
Conclusion
Last Updated: Mar 27, 2024

Dart Runes

Author Akshit Pant
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A Unicode code point is represented by a rune, which is an integer.

In this article, we will be learning about Dart Runes, their applications, use case & methods with an example.

Before learning how we do so, learning the basic Introduction of Dart is recommended.

So, let us start reading about Runes in Dart.

Runes In Dart

A string can be classified as a collection of characters that include letters, numbers, and special characters. In Dart, a string is a collection of Unicode UTF-16 characters. If you want to include 32-bit Unicode characters in a string, you'll need to utilize a special syntax. A Unicode code point is represented by a rune, which is an integer.

The heart character,  for example, is represented by the Unicode counterpart \u2665. Here, The numbers are hexadecimal, which is essentially an integer, and the letter \u stands for Unicode. And place the hex value in curly brackets ( ) if the hex digits are greater or less than four.

Methods in Dart Runes

Dart also has Methods for manipulating Runes that have been built. Below some significant methods are listed.

  • any(bool test(int element)): Checks if any of the elements in this iterable pass the test.
  • contains(Object? element): Returns boolean(true) if the collection includes an element with the same value as the element.
  • elementAt(int index): Element at int index is returned.
  • every(bool test(int element)): Checks if each element of this iterable passes the condition.
  • expand<T>(Iterable<T> toElements(int element)) : Checks if each element of this iterable passes the condition.
  • firstWhere(bool test(int element), {int orElse()?}): The first element that passes the given predicate test is returned.
  • fold<T>(T initialValue, T combine(T previousValue, int element)): Iteratively combines each element of the collection with an existing value to reduce a collection to a single value.
  • followedBy(Iterable<int> other): The lazy concatenation of this(int) iterable and other is returned.
  • forEach(void action(int element)): Invokes action on each element of this iterable in the sequence in which it was created.
  • join([String separator = ""]): Each element is converted to a String, and the strings are concatenated.
  • lastWhere(bool test(int element), {int orElse()?}): The final element that passes the provided predicate test is returned.
  • map<T>(T toElement(int e)): toElement modifies the current elements of this iterable.
  • skipWhile(bool test(int value)): While the test is met, it returns an Iterable that skips the leading components.
  • take(int count): Returns a lazy iterable of this iterable's count first elements.
  • takeWhile(bool test(int value)): This method returns a lazy iterable of the leading components that pass the test.
  • toList({bool growable = true}): This Iterable's elements are collected into a list.
  • toSet(): This method creates a set with the same elements as this iterable.
  • where(bool test(int element)): Returns a new lazy Iterable that contains all of the elements that pass the predicate test.
  • whereType<T>(): Returns a new lazy Iterable containing all T-type items.

Accessing Runes

The dart:core library's String class provides access to runes. The following methods can be utilized to obtain runes:

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

Using String.codeUnits Property

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

Syntax:

Str_name.codeUnits;

Let us see this property with an example given below.

Code:

import 'dart:core'; 
main(){
   String codingNinjas = 'Coding Ninjas Studio';
   print(codingNinjas.codeUnits);
}


Output:

[67, 111, 100, 101, 83, 116, 117, 100, 105, 111]

Using String.runes Property

Iterable is extended by String.runes. This property delivers an iterable of the provided string's Unicode code points.

Syntax:

Str_name.runes;

Let us see this property, too, with an example given below.

Code:

main(){
    String str_name = "Coding Ninjas Studio";
    str_name.runes.forEach((int i) {
       var char = new String.fromCharCode(i);
       print(char);
    });  
 }


Output:

C
o
d
e
S
t
u
d
i
o

Using String.codeUnitAt() Function

It's used to get the UTF-16 code unit at the specified position in the string.

Syntax:

Str_name.codeUnitAt(int index);


Let us see this function’s use with an example given below.

Code:

import 'dart:core';
main(){
   String my_str = 'Coding Ninjas StudioBlogs';
   print(my_str.codeUnitAt(5));
}


Output:

116

Frequently Asked Questions

What is the code unit for the dart string?

Strings are represented in Dart as a series of Unicode UTF-16 code units. And because a Dart string is made up of UTF-16 code units, 32-bit Unicode values are expressed in the string using a specific syntax.

In Dart, what tool is used to format code?

The dart_style package defines a Dart code formatter that is both automatic and opinionated. It replaces the whitespace in our program with the best formatting it can think of.

How long are Dart strings actually?

Memory is the limit for the Dart native VM. (Regarding the size of integers, but on current hardware, 64-bit integers cannot be a limiting issue. Since on any contemporary 64-bit architecture, you can't have more than 2^64 bytes of memory.).

In Dart, what is the integer's default value?

Null will be the default value for integers in the Dart programming language.

Conclusion

In this article, we learned about Dart Runes, their access, and their methods with a working example.
You can head over to our Android Development Course on the Coding Ninjas Website to dive deep into Android Development and build future applications.
We hope this article has helped you enhance your knowledge of Dart programming and Input-Output in it. If you want to learn more, check out our Programming Fundamentals and Competitive Programming articles. Do upvote this article to help other ninjas grow.
Happy Coding!

Live masterclass