On this page11 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. Text Is Stored as Numbers
  4. 2. Writing Special Characters
  5. 3. Runes: Real Characters
  6. 4. Symbols
  7. Code Examples
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 028: Task: Runes & Symbols

Day10: Functions & Parameters
Lecture028 of 057
Estimated video length10 min
TargetDart 3.12
LevelBeginner

Why does 'πŸ˜€'.length say 2? 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):

dart
var word = 'Dart';
print(word.length);           // 4
print(word.codeUnits);        // [68, 97, 114, 116]
print('A'.codeUnitAt(0));     // 65
print(String.fromCharCodes([72, 105]));   // Hi

For 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 { }:

dart
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:

dart
print(smile.length);         // 2
print(smile.runes.length);   // 1

var text = 'Hi πŸ˜€!';
print(text.length);          // 6
print(text.runes.length);    // 5

Loop over runes to see each real character and its number:

dart
for (var r in text.runes) {
  print('$r β†’ ${String.fromCharCode(r)}');
}

Output

code
72 β†’ H
105 β†’ i
32 β†’  
128512 β†’ πŸ˜€
33 β†’ !
PropertyCounts
text.lengthcode units (emoji count as 2)
text.runes.lengthreal characters

4. Symbols

A Symbol stands for a name in your program, written with #:

dart
var s1 = #name;
var s2 = Symbol('name');
print(s1);          // Symbol("name")
print(s1 == s2);    // true
print(#name == #age);   // false

Symbols 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

dart
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

code
"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: Ebsu

Each 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

dart
var s = 'Hi πŸ˜€';
print(s.substring(0, 4));

Output

code
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

  1. What is 'Hi'.codeUnits?
  2. What does String.fromCharCode(65) give?
  3. For 'πŸ˜€πŸ˜€', what are length and runes.length?
  4. Write a string containing a β™₯ using \u.
  5. What does #score == Symbol('score') give?

Practice Answers

  1. [72, 105]
  2. A
  3. 4 and 2.
  4. 'I β™₯ Dart'
  5. 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.
  • runes gives real characters; runes.length counts them correctly.
  • codeUnitAt, String.fromCharCode and fromCharCodes convert between text and numbers.
  • #name is a Symbol, a name in your program, rarely needed day to day.