View source code
							
							
						
								Display the source code in dmd/root/string.d from which thispage was generated on github.
							
						
							Report a bug
							
						
								If you spot a problem with this page, click here to create aBugzilla 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 usinglocal 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
| Name | Description | 
|---|---|
| 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, https://www.digitalmars.com
License
					Copyright © 1999-2025 by the D Language Foundation | Page generated by ddox.