Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 36 · Basics and Types

Characters, Strings and Unicode in D

Learn how D stores text as UTF-8, the difference between char, wchar and dchar, why string is immutable, how to concatenate and slice, and how to count characters correctly.

Text is where many languages quietly go wrong, and D is unusually honest about it. Strings are arrays of UTF-8 code units, so the length of a string is a count of bytes, not of letters. Once you see that, everything else about D strings, from slicing to foreach to the std.uni helpers, falls into place. This lesson explains the three character types, the string types, concatenation, and how to iterate by code unit or by real character.

Three character types

Type Size Meaning
char 8 bits One UTF-8 code unit
wchar 16 bits One UTF-16 code unit
dchar 32 bits One full Unicode code point

A code point is the number Unicode assigns to a character, such as U+00E9 for é. UTF-8 encodes ASCII in one byte and everything else in two to four bytes, so one char is not always one visible character. A dchar always holds a whole code point.

Strings are immutable arrays

The type string is an alias for immutable(char)[]: a slice of characters that cannot be changed. Likewise wstring is immutable(wchar)[] and dstring is immutable(dchar)[]. Literals pick their type by suffix: "abc" is a string, "abc"w a wstring, "abc"d a dstring.

D
import std.stdio;

void main()
{
    string greeting = "Hello";
    string name = "Ada";
    string both = greeting ~ ", " ~ name ~ "!";    // ~ concatenates
    writeln(both);                                  // Hello, Ada!
    writeln(both.length);                           // 11
    writeln(both[0 .. 5]);                          // Hello (a slice, no copy)
    // greeting[0] = 'J';                           // Error: cannot modify immutable
}

~ joins strings (it allocates a new one), and ~= appends. Because strings are immutable, slicing is free and safe: a slice just points into the original characters. To edit text, make a mutable copy: char[] buf = "Hello".dup; (dup copies) and convert back with .idup.

Length is bytes, not characters

D
import std.stdio;
import std.range : walkLength;

void main()
{
    string s = "café";
    writeln(s.length);        // 5: the é uses two bytes
    writeln(s.walkLength);    // 4: counts code points
    foreach (char c; s)       // visits each code unit
    {
        writef("%02x ", c);   // 63 61 66 c3 a9
    }
    writeln();
    foreach (dchar d; s)      // decodes into code points
    {
        write(d, "|");        // c|a|f|é|
    }
    writeln();
}

By default foreach (c; s) over a string uses dchar, which means D decodes UTF-8 for you when you let the variable type be inferred. Spelling out char gives you the raw bytes instead. Slicing with s[a .. b] always counts bytes, and cutting in the middle of a multi-byte character produces invalid UTF-8; the library function std.utf.validate checks for it, and std.uni handles grapheme clusters (what readers see as one character, such as a letter plus an accent).

Useful string functions

Phobos provides most of what you need:

D
import std.stdio;
import std.string : toUpper, strip, split, indexOf, replace;
import std.algorithm : startsWith, canFind;
import std.conv : to;

void main()
{
    string s = "  learn D today  ";
    writeln(s.strip);                    // learn D today
    writeln(s.strip.toUpper);            // LEARN D TODAY
    writeln(s.strip.split(" "));         // ["learn", "D", "today"]
    writeln(s.indexOf("D"));             // 8
    writeln(s.canFind("today"));         // true
    writeln(s.replace("D", "C++"));      //   learn C++ today  
    writeln(42.to!string ~ " apples");   // 42 apples
}

Note the UFCS style (Uniform Function Call Syntax): s.strip means strip(s). You will use it constantly; it is explained further in Function features.

Raw strings avoid escapes: r"C:\temp" and backtick strings such as `C:\temp` keep backslashes literally. q"EOS ... EOS" is a heredoc-style delimited string, and """ is not used. Escape sequences in ordinary strings follow C: \n, \t, \", \\, \u00e9.

C stand-ins: UTF-8 bytes and code points

The byte layout is identical in C, so a C program can show exactly what D's string holds.

C C stand-in: a UTF-8 string is a byte array

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The output is length in bytes: 5 followed by 63 61 66 c3 a9, the same bytes D's foreach (char c; s) visits.

C C stand-in: counting code points like walkLength

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

It prints 3 for abc, 4 for café and 2 for the two-character word 日本. D's decoding foreach and walkLength do the same thing, with full validation.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is the type alias string in D?

    string is an immutable slice of UTF-8 code units.
  2. 2. What is "café".length when the é is encoded as two UTF-8 bytes?

    length counts code units (bytes for string), so it is 5.
  3. 3. Which operator concatenates two strings?

    D uses ~ for concatenation to keep + for numbers.
  4. 4. How do you get a mutable copy of a string?

    dup copies into a mutable char[].

Summary

  • char, wchar and dchar are UTF-8, UTF-16 and UTF-32 code units.
  • string is an immutable slice of char; ~ concatenates and slicing is free.
  • length counts code units; decode with foreach (dchar d; s) or walkLength.
  • Phobos supplies strip, split, replace, toUpper, indexOf and more, usually called with UFCS.

Next: Type conversion and casting