# dmd.root.string

Contains various `string` related functions.

#### Authors

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

#### License

Boost License 1.0

Source: root/string.d

Documentation: https://dlang.org/phobos/dmd_root_string.html

Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/root/string.d

- `toDString`

  #### Declaration

  `pure nothrow @nogc inout(char)[] `toDString`(inout(char)* `s`);`

  Slices a `\0`-terminated C-string, excluding the terminator

- `iequals`

  #### Declaration

  `static pure nothrow @nogc bool `iequals`(const(char)[] `s1`, const(char)[] `s2`);`

  Compare two slices for equality, in a case-insensitive way

  #### Discussion

  Comparison is based on `char` and does not do decoding. As a result, it's only really accurate for plain ASCII strings.

  #### Parameters

  `const(char)[] `s1`` | string to compare
  `const(char)[] `s2`` | string to compare

  #### Return Value

  `true` if ``s1` == `s2`` regardless of case

- `toCStringThen`

  #### Declaration

  `nothrow auto `toCStringThen`(alias dg)(const(char)[] `src`);`

  Copy the content of ``src`` into a C-string ('\0' terminated) then call `dg`

  #### Discussion

  The intent of this function is to provide an allocation-less way to call a C function using a D slice. The function internally allocates a buffer if needed, but frees it on exit.

  Note: The argument to `dg` is `scope`. To keep the data around after `dg` exits, one has to copy it.

  #### Parameters

  `const(char)[] `src`` | Slice to use to call the C function
  `dg` | Delegate to call afterwards

  #### Return Value

  The return value of `T`

- `stripLeadingLineTerminator`

  #### Declaration

  `pure nothrow @nogc @safe string `stripLeadingLineTerminator`(string `str`);`

  Strips one leading line terminator of the given string.

  #### Discussion

  The following are what the Unicode standard considers as line terminators:

  Name | D Escape Sequence | Unicode Code Point
  --- | --- | ---
  Line feed | `\n` | `U+000A`
  Line tabulation | `\v` | `U+000B`
  Form feed | `\f` | `U+000C`
  Carriage return | `\r` | `U+000D`
  Next line |  | `U+0085`
  Line separator |  | `U+2028`
  Paragraph separator |  | `U+2029`

  This function will also strip `\r\n`.
- `dstrcmp`

  #### Declaration

  `@trusted int `dstrcmp`()(scope const char[] `s1`, scope const char[] `s2`);`

  A string comparison functions that returns the same result as strcmp

  Note: Strings are compared based on their ASCII values, no UTF-8 decoding.
  Some C functions (e.g. `qsort`) require a `int` result for comparison.

  #### See Also

  Druntime's `core.internal.string`

- `toStaticArray`

  #### Declaration

  `char[N + 1] `toStaticArray`(size_t N)(scope const(char)[N] `literal`);`

  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.

  #### Parameters

  `const(char)[N] `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)

  #### Examples

  1. `auto m = "123".toStaticArray; const c = "123".toStaticArray; immutable i = "123".toStaticArray; enum e = "123".toStaticArray; assert(m == "123\0"); assert(c == "123\0"); assert(i == "123\0"); static assert(e == "123\0"); const empty = "".toStaticArray; static assert(empty.length == 1); static assert(empty[0] == '\0');`
- `startsWith`

  #### Declaration

  `pure nothrow @nogc @system bool `startsWith`(scope const(char)* `p`, scope const(char)[] `needle`);`

  Checks if C string ``p`` starts with ``needle``.

  #### Parameters

  `const(char)* `p`` | the C string to check
  `const(char)[] `needle`` | the string to look for

  #### Return Value

  `true` if ``p`` starts with ``needle``

  #### Examples

  1. `const buf = "123".toStaticArray; const ptr = &buf[0]; assert(ptr.startsWith("")); assert(ptr.startsWith("1")); assert(ptr.startsWith("12")); assert(ptr.startsWith("123")); assert(!ptr.startsWith("1234"));`