View source code
Display the source code in dmd/root/string.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function dmd.root.string.toStaticArray

Infers the length N of a string literal and coerces its type to a static array with length N + 1. Returns the string with a null character appended to the end.

char[N+1] toStaticArray(ulong N) (
  scope const(char)[N] literal


literal string literal


- LDC produces quite optimal code for short strings: - - (backup URL)


auto m = "123".toStaticArray;
const c = "123".toStaticArray;
immutable i = "123".toStaticArray;
enum e = "123".toStaticArray;

writeln(m); // "123\0"
writeln(c); // "123\0"
writeln(i); // "123\0"
static assert(e == "123\0");

const empty = "".toStaticArray;
static assert(empty.length == 1);
static assert(empty[0] == '\0');


Walter Bright,


Boost License 1.0