Secrets Of Strings in Dart

Debanshu Datta
Backyard Programmers
3 min readJun 13, 2022
Photo by Nicolas Horn on Unsplash

Let’s dive right into it we will start by understanding what are Runes in Dart,

Runes are a collection formed out of a String's Unicode decimal code points. A Code Point is an integer value that uniquely identifies a given Unicode Character from inside the String

A Dart String is a sequence of UTF-16 code. A Code Unit is the unit of an Encoded Code Point .

UTF-16 means the code point of a Unicode Character that has been Encoded to 16-bit Code Unit (Hexadecimal Code Point). Then the code point is a hexadecimal number, which means in dart every string is split between characters and each of these characters has a hexadecimal code point, behind the scenes in Dart, a string is no more than a list of hexadecimal code points.

String s='Helloooo' ---> 'H','e','l','l','o','o','o','o'
0048,0065,006C,006C,006F,006F,006F,006F

Let’s See Some Examples

We make variable runes, we can actually see a list ofutf-16 code points for that string

now, let's convert it into decimal code points in order to see its hexadecimal values, we can call toRedixSting() and set 16 bit as the base, then pad it to the left so that it will always be 4 characters long, we are using a map to call it to all characters.

By the way, Dart is amazing in providing interpolation of this Unicode to characters with the /u{} delimiter i.e, any hexadecimal Unicode can be converted including emojis.

For a character outside the Basic Multilingual Plane (plane 0) that is composed of a surrogate pair, runes combine the pair and return a single integer. For example, the Unicode character for a musical G-clef (‘𝄞’) with rune value 0x1D11E consists of a UTF-16 surrogate pair: 0xD834 and 0xDD1E. Using codeUnits returns the surrogate pair, and using runes returns their combined value:

For any doubts and suggestions, you can reach out on my Instagram, or LinkedIn. Follow me for Dart & Kotlin content and more. Happy Coding!

I will well appreciate one of these 👏

Recent Post

--

--

Debanshu Datta
Backyard Programmers

Android(L2) @Gojek | Mobile Developer | Backend Developer (Java/Kotlin)