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
);

Parameters

NameDescription
literal string literal

Notes

- LDC produces quite optimal code for short strings: - https://d.godbolt.org/z/M69Z1g - https://gist.github.com/PetarKirov/338e4ab9292b6b2b311a3070572a07fb (backup URL)

Example

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');

Authors

Walter Bright, http://www.digitalmars.com

License

Boost License 1.0