On this page11 sections
Lecture 028: Task: Runes & Symbols
| Day | 10: Functions & Parameters |
| Lecture | 028 of 057 |
| Estimated video length | 10 min |
| Target | Dart 3.12 |
| Level | Beginner |
Why does
'π'.lengthsay2? Text is stored as numbers, and some characters (emoji, many symbols) need two of them. Runes let you work with real characters. Symbols are a different, rarely needed feature: a way to name parts of your program itself.
Contents
Prerequisites Β· Objectives Β· Lesson Β· Code Examples Β· Common Mistakes Β· Practice Questions Β· Answers Β· Takeaways
Prerequisites
Lectures 009 (strings), 011 (lists) and 020 (for-in loops).
Learning Objectives
- Explain code units and runes (Unicode code points)
- Write special characters with
\u - Count real characters with
runes.length - Convert between characters and numbers
- Recognise a
Symbol(#name)
1. Text Is Stored as Numbers
Every character has a number. Dart strings store code units (16-bit numbers):
var word = 'Dart';
print(word.length); // 4
print(word.codeUnits); // [68, 97, 114, 116]
print('A'.codeUnitAt(0)); // 65
print(String.fromCharCodes([72, 105])); // HiFor ordinary letters, one character = one code unit.
2. Writing Special Characters
Use \u followed by the character's code in hex: four digits, or any number of digits inside
{ }:
var heart = 'β₯';
print(heart); // β₯
var smile = '\u{1F600}';
print(smile); // π3. Runes: Real Characters
A rune is one full Unicode character (a code point). Emoji don't fit in one 16-bit code
unit, so they take two, and length counts code units, not characters:
print(smile.length); // 2
print(smile.runes.length); // 1
var text = 'Hi π!';
print(text.length); // 6
print(text.runes.length); // 5Loop over runes to see each real character and its number:
for (var r in text.runes) {
print('$r β ${String.fromCharCode(r)}');
}Output
72 β H
105 β i
32 β
128512 β π
33 β !| Property | Counts |
|---|---|
text.length | code units (emoji count as 2) |
text.runes.length | real characters |
4. Symbols
A Symbol stands for a name in your program, written with #:
var s1 = #name;
var s2 = Symbol('name');
print(s1); // Symbol("name")
print(s1 == s2); // true
print(#name == #age); // falseSymbols are used by advanced tools that inspect code (and some libraries). You'll rarely write
one yourself, but now you'll recognise #something if you see it.
Code Examples
Example 1: Counting Characters in Messages
void main() {
var messages = ['Hello', 'Salam π', 'Good job ππ', 'I β€ Dart'];
for (var m in messages) {
print('"$m": length ${m.length}, characters ${m.runes.length}');
}
var word = 'Dart';
var codes = word.runes.toList();
print('Codes for $word: $codes');
var shifted = <int>[];
for (var c in codes) {
shifted.add(c + 1);
}
print('Secret code: ${String.fromCharCodes(shifted)}');
}Output
"Hello": length 5, characters 5
"Salam π": length 8, characters 7
"Good job ππ": length 13, characters 11
"I β€ Dart": length 8, characters 8
Codes for Dart: [68, 97, 114, 116]
Secret code: EbsuEach emoji adds 2 to length but 1 to the character count. β€ fits in one code unit, so it
counts as 1 either way. Adding 1 to each code turns D-a-r-t into E-b-s-u: a simple secret code.
Common Beginner Mistakes
1. Cutting an emoji in half
var s = 'Hi π';
print(s.substring(0, 4));Output
Hi οΏ½substring counts code units, and index 4 falls in the middle of the emoji, leaving a broken
character. Be careful slicing text that may contain emoji.
2. Using length as "number of characters": for user-typed text (names, chat messages), a
character limit based on length counts each emoji twice. Use runes.length.
3. Confusing a Symbol with a String: #name is not 'name'; #name == 'name' is
false.
Practice Questions
- What is
'Hi'.codeUnits? - What does
String.fromCharCode(65)give? - For
'ππ', what arelengthandrunes.length? - Write a string containing a β₯ using
\u. - What does
#score == Symbol('score')give?
Practice Answers
[72, 105]A4and2.'I β₯ Dart'true
Key Takeaways
- Strings are stored as 16-bit code units; most characters use one, emoji use two.
β₯or\u{1F600}writes a character by its code.runesgives real characters;runes.lengthcounts them correctly.codeUnitAt,String.fromCharCodeandfromCharCodesconvert between text and numbers.#nameis aSymbol, a name in your program, rarely needed day to day.