D Programming Language
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. Page wiki View or edit the community-maintained wiki page associated with this page.

# std.string

String handling functions. Note that many typical string functions are found in std.algorithm because all D strings are bidirectional ranges.

Category | Functions
--- | ---
Searching | indexOf lastIndexOf indexOfAny lastIndexOfAny indexOfNeither lastIndexOfNeither std.algorithm.startsWith std.algorithm.endsWith inPattern column std.algorithm search functions

Comparison | std.algorithm.cmp isNumeric countchars std.uni comparison functions std.ascii comparison functions

Mutation | toLower toLowerInPlace toUpper toUpperInPlace capitalize removechars squeeze munch std.range.retro

Pruning and Filling | stripLeft stripRight strip chomp chompPrefix chop leftJustify rightJustify center detab entab wrap outdent popFront std.array.front std.array.popBack std.array.back

Substitution | translate soundex abbrev succ tr std.array.replace std.array.replaceInPlace

Miscellaneous | fromStringz toStringz splitLines representation assumeUTF

Objects of types string, wstring, and dstring are value types and cannot be mutated element-by-element. For using mutation during building strings, use char[], wchar[], or dchar[]. The xxxstring types are preferable because they don't exhibit undesired aliasing, thus making code more robust.

Not all functions for D string handling are in this module. Functions related to Unicode and ASCII are found in std.uni and std.ascii, respectively. Other functions that have a wider generality than just strings can be found in std.algorithm and std.range.

Functions std.array.join std.array.replace std.array.replaceInPlace std.array.split std.uni.icmp std.uni.toLower std.uni.toLowerInPlace std.uni.toUpper std.uni.toUpperInPlace std.format.format are publicly imported.
License:
Boost License 1.0.
Authors:
Walter Bright, Andrei Alexandrescu, and Jonathan M Davis

Source: std/string.d

class StringException: object.Exception;
  Exception thrown on errors in std.string functions.
  pure nothrow @safe this(string msg, string file = __FILE__, size_t line = __LINE__, Throwable next = null);
    Parameters:

    string msg | The message for the exception.
    string file | The file where the exception occurred.
    size_t line | The line number where the exception occurred.
    Throwable next | The previous exception in the chain of exceptions, if any.

pure @system inout(char)[] fromStringz(inout(char)* cString);
  Returns a D-style array of char given a zero-terminated C-style string. The returned array will retain the same type qualifiers as the input.
  Important Note: The returned array is a slice of the original buffer. The original data is not changed and not copied.
  Examples:

      assert(fromStringz(null) == null);
      assert(fromStringz("foo") == "foo");

pure nothrow @trusted immutable(char)* toStringz(const(char)[] s);
pure nothrow @trusted immutable(char)* toStringz(in string s);
  Returns a C-style zero-terminated string equivalent to s. s must not contain embedded '\0''s as any C function will treat the first '\0' that it sees as the end of the string. If s.empty is true, then a string containing only '\0' is returned.
  Important Note: When passing a char* to a C function, and the C function keeps it around for any reason, make sure that you keep a reference to it in your D code. Otherwise, it may go away during a garbage collection cycle and cause a nasty bug when the C code tries to use it.
alias CaseSensitive = std.typecons.Flag!"caseSensitive".Flag;
  Flag indicating whether a search is case-sensitive.
pure @safe ptrdiff_t indexOf(Char)(in Char[] s, in dchar c, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char);
  Returns the index of the first occurrence of c in s. If c is not found, then -1 is returned.
  cs indicates whether the comparisons are case sensitive.
pure @safe ptrdiff_t indexOf(Char)(const(Char)[] s, in dchar c, in size_t startIdx, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char);
  Returns the index of the first occurrence of c in s with respect to the start index startIdx. If c is not found, then -1 is returned. If c is found the value of the returned index is at least startIdx. startIdx represents a codeunit index in s. If the sequence starting at startIdx does not represent a well formed codepoint, then a std.utf.UTFException may be thrown.
  cs indicates whether the comparisons are case sensitive.
@trusted ptrdiff_t indexOf(Char1, Char2)(const(Char1)[] s, const(Char2)[] sub, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char1 && isSomeChar!Char2);
  Returns the index of the first occurrence of sub in s. If sub is not found, then -1 is returned.
  cs indicates whether the comparisons are case sensitive.
@safe ptrdiff_t indexOf(Char1, Char2)(const(Char1)[] s, const(Char2)[] sub, in size_t startIdx, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char1 && isSomeChar!Char2);
  Returns the index of the first occurrence of sub in s with respect to the start index startIdx. If sub is not found, then -1 is returned. If sub is found the value of the returned index is at least startIdx. startIdx represents a codeunit index in s. If the sequence starting at startIdx does not represent a well formed codepoint, then a std.utf.UTFException may be thrown.
  cs indicates whether the comparisons are case sensitive.
pure @safe ptrdiff_t lastIndexOf(Char)(const(Char)[] s, in dchar c, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char);
  Returns the index of the last occurrence of c in s. If c is not found, then -1 is returned.
  cs indicates whether the comparisons are case sensitive.
pure @safe ptrdiff_t lastIndexOf(Char)(const(Char)[] s, in dchar c, in size_t startIdx, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char);
  Returns the index of the last occurrence of c in s. If c is not found, then -1 is returned. The startIdx slices s in the following way s[0 .. startIdx]. startIdx represents a codeunit index in s. If the sequence ending at startIdx does not represent a well formed codepoint, then a std.utf.UTFException may be thrown.
  cs indicates whether the comparisons are case sensitive.
pure @safe ptrdiff_t lastIndexOf(Char1, Char2)(const(Char1)[] s, const(Char2)[] sub, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char1 && isSomeChar!Char2);
  Returns the index of the last occurrence of sub in s. If sub is not found, then -1 is returned.
  cs indicates whether the comparisons are case sensitive.
pure @safe ptrdiff_t lastIndexOf(Char1, Char2)(const(Char1)[] s, const(Char2)[] sub, in size_t startIdx, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char1 && isSomeChar!Char2);
  Returns the index of the last occurrence of sub in s. If sub is not found, then -1 is returned. The startIdx slices s in the following way s[0 .. startIdx]. startIdx represents a codeunit index in s. If the sequence ending at startIdx does not represent a well formed codepoint, then a std.utf.UTFException may be thrown.
  cs indicates whether the comparisons are case sensitive.
pure @safe ptrdiff_t indexOfAny(Char, Char2)(const(Char)[] haystack, const(Char2)[] needles, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char && isSomeChar!Char2);
  Returns the index of the first occurence of any of the elements in needles in haystack. If no element of needles is found, then -1 is returned.
  Parameters:

  const(Char)[] haystack | String to search for needles in.
  const(Char2)[] needles | Strings to search for in haystack.
  CaseSensitive cs | Indicates whether the comparisons are case sensitive.

  Examples:

      import std.conv : to;

      ptrdiff_t i = "helloWorld".indexOfAny("Wr");
      assert(i == 5);
      i = "öällo world".indexOfAny("lo ");
      assert(i == 4, to!string(i));

pure @safe ptrdiff_t indexOfAny(Char, Char2)(const(Char)[] haystack, const(Char2)[] needles, in size_t startIdx, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char && isSomeChar!Char2);
  Returns the index of the first occurence of any of the elements in needles in haystack. If no element of needles is found, then -1 is returned. The startIdx slices s in the following way haystack[startIdx .. $]. startIdx represents a codeunit index in haystack. If the sequence ending at startIdx does not represent a well formed codepoint, then a std.utf.UTFException may be thrown.
  Parameters:

  const(Char)[] haystack | String to search for needles in.
  const(Char2)[] needles | Strings to search for in haystack.
  size_t startIdx | slices haystack like this haystack[startIdx .. $]. If the startIdx is greater equal the length of haystack the functions returns -1.
  CaseSensitive cs | Indicates whether the comparisons are case sensitive.

  Examples:

      import std.conv : to;

      ptrdiff_t i = "helloWorld".indexOfAny("Wr", 4);
      assert(i == 5);

      i = "Foo öällo world".indexOfAny("lh", 3);
      assert(i == 8, to!string(i));

pure @safe ptrdiff_t lastIndexOfAny(Char, Char2)(const(Char)[] haystack, const(Char2)[] needles, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char && isSomeChar!Char2);
  Returns the index of the last occurence of any of the elements in needles in haystack. If no element of needles is found, then -1 is returned.
  Parameters:

  const(Char)[] haystack | String to search for needles in.
  const(Char2)[] needles | Strings to search for in haystack.
  CaseSensitive cs | Indicates whether the comparisons are case sensitive.

  Examples:

      ptrdiff_t i = "helloWorld".lastIndexOfAny("Wlo");
      assert(i == 8);

      i = "Foo öäöllo world".lastIndexOfAny("öF");
      assert(i == 8);

pure @safe ptrdiff_t lastIndexOfAny(Char, Char2)(const(Char)[] haystack, const(Char2)[] needles, in size_t stopIdx, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char && isSomeChar!Char2);
  Returns the index of the last occurence of any of the elements in needles in haystack. If no element of needles is found, then -1 is returned. The stopIdx slices s in the following way s[0 .. stopIdx]. stopIdx represents a codeunit index in s. If the sequence ending at startIdx does not represent a well formed codepoint, then a std.utf.UTFException may be thrown.
  Parameters:

  const(Char)[] haystack | String to search for needles in.
  const(Char2)[] needles | Strings to search for in haystack.
  size_t stopIdx | slices haystack like this haystack[0 .. stopIdx]. If the stopIdx is greater equal the length of haystack the functions returns -1.
  CaseSensitive cs | Indicates whether the comparisons are case sensitive.

  Examples:

      import std.conv : to;

      ptrdiff_t i = "helloWorld".lastIndexOfAny("Wlo", 4);
      assert(i == 3);

      i = "Foo öäöllo world".lastIndexOfAny("öF", 3);
      assert(i == 0);

pure @safe ptrdiff_t indexOfNeither(Char, Char2)(const(Char)[] haystack, const(Char2)[] needles, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char && isSomeChar!Char2);
  Returns the index of the first occurence of any character not an elements in needles in haystack. If all element of haystack are element of needles -1 is returned.
  Parameters:

  const(Char)[] haystack | String to search for needles in.
  const(Char2)[] needles | Strings to search for in haystack.
  CaseSensitive cs | Indicates whether the comparisons are case sensitive.

  Examples:

      assert(indexOfNeither("def", "a") == 0);
      assert(indexOfNeither("def", "de") == 2);
      assert(indexOfNeither("dfefffg", "dfe") == 6);

pure @safe ptrdiff_t indexOfNeither(Char, Char2)(const(Char)[] haystack, const(Char2)[] needles, in size_t startIdx, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char && isSomeChar!Char2);
  Returns the index of the first occurence of any character not an elements in needles in haystack. If all element of haystack are element of needles -1 is returned.
  Parameters:

  const(Char)[] haystack | String to search for needles in.
  const(Char2)[] needles | Strings to search for in haystack.
  size_t startIdx | slices haystack like this haystack[startIdx .. $]. If the startIdx is greater equal the length of haystack the functions returns -1.
  CaseSensitive cs | Indicates whether the comparisons are case sensitive.

  Examples:

      assert(indexOfNeither("abba", "a", 2) == 2);
      assert(indexOfNeither("def", "de", 1) == 2);
      assert(indexOfNeither("dfefffg", "dfe", 4) == 6);

pure @safe ptrdiff_t lastIndexOfNeither(Char, Char2)(const(Char)[] haystack, const(Char2)[] needles, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char && isSomeChar!Char2);
  Returns the last index of the first occurence of any character that is not an elements in needles in haystack. If all element of haystack are element of needles -1 is returned.
  Parameters:

  const(Char)[] haystack | String to search for needles in.
  const(Char2)[] needles | Strings to search for in haystack.
  CaseSensitive cs | Indicates whether the comparisons are case sensitive.

  Examples:

      assert(lastIndexOfNeither("abba", "a") == 2);
      assert(lastIndexOfNeither("def", "f") == 1);

pure @safe ptrdiff_t lastIndexOfNeither(Char, Char2)(const(Char)[] haystack, const(Char2)[] needles, in size_t stopIdx, in CaseSensitive cs = CaseSensitive.yes) if (isSomeChar!Char && isSomeChar!Char2);
  Returns the last index of the first occurence of any character that is not an elements in needles in haystack. If all element of haystack are element of needles -1 is returned.
  Parameters:

  const(Char)[] haystack | String to search for needles in.
  const(Char2)[] needles | Strings to search for in haystack.
  size_t stopIdx | slices haystack like this haystack[0 .. stopIdx] If the stopIdx is greater equal the length of haystack the functions returns -1.
  CaseSensitive cs | Indicates whether the comparisons are case sensitive.

  Examples:

      assert(lastIndexOfNeither("def", "rsa", 3) == -1);
      assert(lastIndexOfNeither("abba", "a", 2) == 1);

pure nothrow @nogc @safe auto representation(Char)(Char[] s) if (isSomeChar!Char);
  Returns the representation of a string, which has the same type as the string except the character type is replaced by ubyte, ushort, or uint depending on the character width.
  Examples:

      string s = "hello";
      static assert(is(typeof(representation(s)) == immutable(ubyte)[]));
      assert(representation(s) is cast(immutable(ubyte)[]) s);
      assert(representation(s) == [0x68, 0x65, 0x6c, 0x6c, 0x6f]);

pure @trusted S capitalize(S)(S s) if (isSomeString!S);
  Capitalize the first character of s and convert the rest of s to lowercase.
alias KeepTerminator = std.typecons.Flag!"keepTerminator".Flag;
pure @safe S[] splitLines(S)(S s, in KeepTerminator keepTerm = KeepTerminator.no) if (isSomeString!S);
  Split s into an array of lines using '\r', '\n', "\r\n", std.uni.lineSep, and std.uni.paraSep as delimiters. If keepTerm is set to KeepTerminator.yes, then the delimiter is included in the strings returned.
  Does not throw on invalid UTF; such is simply passed unchanged to the output.

  Allocates memory; use lineSplitter for an alternative that does not.
  Parameters:

  s | a string of chars, wchars, or dchars
  keepTerm | whether delimiter is included or not in the results

  Returns:
  array of strings, each element is a line that is a slice of s
  See Also:
  lineSplitter std.algorithm.splitter std.regex.splitter
auto lineSplitter(KeepTerminator keepTerm = KeepTerminator.no, Range)(Range r) if (hasSlicing!Range && hasLength!Range || isSomeString!Range);
  Split an array or slicable range of characters into a range of lines using '\r', '\n', "\r\n", std.uni.lineSep, and std.uni.paraSep as delimiters. If keepTerm is set to KeepTerminator.yes, then the delimiter is included in the slices returned.
  Does not throw on invalid UTF; such is simply passed unchanged to the output.

  Does not allocate memory.
  Parameters:

  Range r | array of chars, wchars, or dchars or a slicable range
  keepTerm | whether delimiter is included or not in the results

  Returns:
  range of slices of the input range r
  See Also:
  splitLines std.algorithm.splitter std.regex.splitter
  Examples:

      auto s = "\rpeter\n\rpaul\r\njerry\u2028ice\u2029cream\n\nsunday\nmon\u2030day\n";
      auto lines = s.lineSplitter();
      static immutable witness = ["", "peter", "", "paul", "jerry", "ice", "cream", "", "sunday", "mon\u2030day"];
      uint i;
      foreach (line; lines)
      {
          assert(line == witness[i++]);
      }
      assert(i == witness.length);

pure @nogc @safe C[] stripLeft(C)(C[] str) if (isSomeChar!C);
  Strips leading whitespace (as defined by std.uni.isWhite).
  Returns:
  str stripped of leading whitespace.

  Postconditions: str and the returned value will share the same tail (see std.array.sameTail).

  Examples:

      import std.uni : lineSep, paraSep;
      assert(stripLeft("     hello world     ") ==
             "hello world     ");
      assert(stripLeft("\n\t\v\rhello world\n\t\v\r") ==
             "hello world\n\t\v\r");
      assert(stripLeft("hello world") ==
             "hello world");
      assert(stripLeft([lineSep] ~ "hello world" ~ lineSep) ==
             "hello world" ~ [lineSep]);
      assert(stripLeft([paraSep] ~ "hello world" ~ paraSep) ==
             "hello world" ~ [paraSep]);

pure @nogc @safe C[] stripRight(C)(C[] str) if (isSomeChar!C);
  Strips trailing whitespace (as defined by std.uni.isWhite).
  Returns:
  str stripped of trailing whitespace.

  Postconditions: str and the returned value will share the same head (see std.array.sameHead).

  Examples:

      import std.uni : lineSep, paraSep;
      assert(stripRight("     hello world     ") ==
             "     hello world");
      assert(stripRight("\n\t\v\rhello world\n\t\v\r") ==
             "\n\t\v\rhello world");
      assert(stripRight("hello world") ==
             "hello world");
      assert(stripRight([lineSep] ~ "hello world" ~ lineSep) ==
             [lineSep] ~ "hello world");
      assert(stripRight([paraSep] ~ "hello world" ~ paraSep) ==
             [paraSep] ~ "hello world");

pure @safe C[] strip(C)(C[] str) if (isSomeChar!C);
  Strips both leading and trailing whitespace (as defined by std.uni.isWhite).
  Returns:
  str stripped of trailing whitespace.
  Examples:

      import std.uni : lineSep, paraSep;
      assert(strip("     hello world     ") ==
             "hello world");
      assert(strip("\n\t\v\rhello world\n\t\v\r") ==
             "hello world");
      assert(strip("hello world") ==
             "hello world");
      assert(strip([lineSep] ~ "hello world" ~ [lineSep]) ==
             "hello world");
      assert(strip([paraSep] ~ "hello world" ~ [paraSep]) ==
             "hello world");

pure nothrow @nogc @safe C[] chomp(C)(C[] str) if (isSomeChar!C);
pure @safe C1[] chomp(C1, C2)(C1[] str, const(C2)[] delimiter) if (isSomeChar!C1 && isSomeChar!C2);
  If str ends with delimiter, then str is returned without delimiter on its end. If it str does not end with delimiter, then it is returned unchanged.
  If no delimiter is given, then one trailing '\r', '\n', "\r\n", std.uni.lineSep, or std.uni.paraSep is removed from the end of str. If str does not end with any of those characters, then it is returned unchanged.
  Examples:

      import std.utf : decode;
      import std.uni : lineSep, paraSep;
      assert(chomp(" hello world  \n\r") == " hello world  \n");
      assert(chomp(" hello world  \r\n") == " hello world  ");
      assert(chomp(" hello world  \n\n") == " hello world  \n");
      assert(chomp(" hello world  \n\n ") == " hello world  \n\n ");
      assert(chomp(" hello world  \n\n" ~ [lineSep]) == " hello world  \n\n");
      assert(chomp(" hello world  \n\n" ~ [paraSep]) == " hello world  \n\n");
      assert(chomp(" hello world") == " hello world");
      assert(chomp("") == "");

      assert(chomp(" hello world", "orld") == " hello w");
      assert(chomp(" hello world", " he") == " hello world");
      assert(chomp("", "hello") == "");

      // Don't decode pointlessly
      assert(chomp("hello\xFE", "\r") == "hello\xFE");

pure @safe C1[] chompPrefix(C1, C2)(C1[] str, C2[] delimiter) if (isSomeChar!C1 && isSomeChar!C2);
  If str starts with delimiter, then the part of str following delimiter is returned. If it str does not start with delimiter, then it is returned unchanged.
  Examples:

      assert(chompPrefix("hello world", "he") == "llo world");
      assert(chompPrefix("hello world", "hello w") == "orld");
      assert(chompPrefix("hello world", " world") == "hello world");
      assert(chompPrefix("", "hello") == "");

pure @safe S chop(S)(S str) if (isSomeString!S);
  Returns str without its last character, if there is one. If str ends with "\r\n", then both are removed. If str is empty, then then it is returned unchanged.
  Examples:

      assert(chop("hello world") == "hello worl");
      assert(chop("hello world\n") == "hello world");
      assert(chop("hello world\r") == "hello world");
      assert(chop("hello world\n\r") == "hello world\n");
      assert(chop("hello world\r\n") == "hello world");
      assert(chop("Walter Bright") == "Walter Brigh");
      assert(chop("") == "");

pure @trusted S leftJustify(S)(S s, size_t width, dchar fillChar = ' ') if (isSomeString!S);
  Left justify s in a field width characters wide. fillChar is the character that will be used to fill up the space in the field that s doesn't fill.
pure @trusted S rightJustify(S)(S s, size_t width, dchar fillChar = ' ') if (isSomeString!S);
  Right justify s in a field width characters wide. fillChar is the character that will be used to fill up the space in the field that s doesn't fill.
pure @trusted S center(S)(S s, size_t width, dchar fillChar = ' ') if (isSomeString!S);
  Center s in a field width characters wide. fillChar is the character that will be used to fill up the space in the field that s doesn't fill.
pure @trusted S detab(S)(S s, size_t tabSize = 8) if (isSomeString!S);
  Replace each tab character in s with the number of spaces necessary to align the following character at the next tab stop where tabSize is the distance between tab stops.
pure @trusted S entab(S)(S s, size_t tabSize = 8) if (isSomeString!S);
  Replaces spaces in s with the optimal number of tabs. All spaces and tabs at the end of a line are removed.
  Parameters:

  S s | String to convert.
  size_t tabSize | Tab columns are tabSize spaces apart.

pure @safe C1[] translate(C1, C2 = immutable(char))(C1[] str, in dchar[dchar] transTable, const(C2)[] toRemove = null) if (isSomeChar!C1 && isSomeChar!C2);
pure @safe C1[] translate(C1, S, C2 = immutable(char))(C1[] str, in S[dchar] transTable, const(C2)[] toRemove = null) if (isSomeChar!C1 && isSomeString!S && isSomeChar!C2);
  Replaces the characters in str which are keys in transTable with their corresponding values in transTable. transTable is an AA where its keys are dchar and its values are either dchar or some type of string. Also, if toRemove is given, the characters in it are removed from str prior to translation. str itself is unaltered. A copy with the changes is returned.
  See Also:
  tr std.array.replace
  Parameters:

  C1[] str | The original string.
  dchar[dchar] transTable | The AA indicating which characters to replace and what to replace them with.
  const(C2)[] toRemove | The characters to remove from the string.

  Examples:

      dchar[dchar] transTable1 = ['e' : '5', 'o' : '7', '5': 'q'];
      assert(translate("hello world", transTable1) == "h5ll7 w7rld");

      assert(translate("hello world", transTable1, "low") == "h5 rd");

      string[dchar] transTable2 = ['e' : "5", 'o' : "orange"];
      assert(translate("hello world", transTable2) == "h5llorange worangerld");

void translate(C1, C2 = immutable(char), Buffer)(C1[] str, in dchar[dchar] transTable, const(C2)[] toRemove, Buffer buffer) if (isSomeChar!C1 && isSomeChar!C2 && isOutputRange!(Buffer, C1));
void translate(C1, S, C2 = immutable(char), Buffer)(C1[] str, in S[dchar] transTable, const(C2)[] toRemove, Buffer buffer) if (isSomeChar!C1 && isSomeString!S && isSomeChar!C2 && isOutputRange!(Buffer, S));
  This is an overload of translate which takes an existing buffer to write the contents to.
  Parameters:

  C1[] str | The original string.
  dchar[dchar] transTable | The AA indicating which characters to replace and what to replace them with.
  const(C2)[] toRemove | The characters to remove from the string.
  Buffer buffer | An output range to write the contents to.

  Examples:

      import std.array : appender;
      dchar[dchar] transTable1 = ['e' : '5', 'o' : '7', '5': 'q'];
      auto buffer = appender!(dchar[])();
      translate("hello world", transTable1, null, buffer);
      assert(buffer.data == "h5ll7 w7rld");

      buffer.clear();
      translate("hello world", transTable1, "low", buffer);
      assert(buffer.data == "h5 rd");

      buffer.clear();
      string[dchar] transTable2 = ['e' : "5", 'o' : "orange"];
      translate("hello world", transTable2, null, buffer);
      assert(buffer.data == "h5llorange worangerld");

pure nothrow @trusted C[] translate(C = immutable(char))(in char[] str, in char[] transTable, in char[] toRemove = null) if (is(Unqual!C == char));
pure nothrow @trusted string makeTrans(in char[] from, in char[] to);
  This is an ASCII-only overload of translate. It will not work with Unicode. It exists as an optimization for the cases where Unicode processing is not necessary.
  Unlike the other overloads of translate, this one does not take an AA. Rather, it takes a string generated by makeTrans.

  The array generated by makeTrans is 256 elements long such that the index is equal to the ASCII character being replaced and the value is equal to the character that it's being replaced with. Note that translate does not decode any of the characters, so you can actually pass it Extended ASCII characters if you want to (ASCII only actually uses 128 characters), but be warned that Extended ASCII characters are not valid Unicode and therefore will result in a UTFException being thrown from most other Phobos functions.

  Also, because no decoding occurs, it is possible to use this overload to translate ASCII characters within a proper UTF-8 string without altering the other, non-ASCII characters. It's replacing any code unit greater than 127 with another code unit or replacing any code unit with another code unit greater than 127 which will cause UTF validation issues.
  See Also:
  tr std.array.replace
  Parameters:

  char[] str | The original string.
  char[] transTable | The string indicating which characters to replace and what to replace them with. It is generated by makeTrans.
  char[] toRemove | The characters to remove from the string.

  Examples:

      auto transTable1 = makeTrans("eo5", "57q");
      assert(translate("hello world", transTable1) == "h5ll7 w7rld");

      assert(translate("hello world", transTable1, "low") == "h5 rd");

pure @trusted void translate(C = immutable(char), Buffer)(in char[] str, in char[] transTable, in char[] toRemove, Buffer buffer) if (is(Unqual!C == char) && isOutputRange!(Buffer, char));
  This is an ASCII-only overload of translate which takes an existing buffer to write the contents to.
  Parameters:

  char[] str | The original string.
  char[] transTable | The string indicating which characters to replace and what to replace them with. It is generated by makeTrans.
  char[] toRemove | The characters to remove from the string.
  Buffer buffer | An output range to write the contents to.

  Examples:

      import std.array : appender;
      auto buffer = appender!(char[])();
      auto transTable1 = makeTrans("eo5", "57q");
      translate("hello world", transTable1, null, buffer);
      assert(buffer.data == "h5ll7 w7rld");

      buffer.clear();
      translate("hello world", transTable1, "low", buffer);
      assert(buffer.data == "h5 rd");

pure @nogc @safe bool inPattern(S)(dchar c, in S pattern) if (isSomeString!S);
  See if character c is in the pattern.

  Patterns: A pattern is an array of characters much like a character class in regular expressions. A sequence of characters can be given, such as "abcde". The '-' can represent a range of characters, as "a-e" represents the same pattern as "abcde". "a-fA-F0-9" represents all the hex characters. If the first character of a pattern is '^', then the pattern is negated, i.e. "^0-9" means any character except a digit. The functions inPattern, countchars, removeschars, and squeeze use patterns.

  Note: In the future, the pattern syntax may be improved to be more like regular expression character classes.

pure @nogc @safe bool inPattern(S)(dchar c, S[] patterns) if (isSomeString!S);
  See if character c is in the intersection of the patterns.
pure @nogc @safe size_t countchars(S, S1)(S s, in S1 pattern) if (isSomeString!S && isSomeString!S1);
  Count characters in s that match pattern.
pure @safe S removechars(S)(S s, in S pattern) if (isSomeString!S);
  Return string that is s with all characters removed that match pattern.
S squeeze(S)(S s, in S pattern = null);
  Return string where sequences of a character in s[] from pattern[] are replaced with a single instance of that character. If pattern is null, it defaults to all characters.
pure @nogc @safe S1 munch(S1, S2)(ref S1 s, S2 pattern);
  Finds the position pos of the first character in s that does not match pattern (in the terminology used by inPattern). Updates s = s[pos..$]. Returns the slice from the beginning of the original (before update) string up to, and excluding, pos.
  The munch function is mostly convenient for skipping certain category of characters (e.g. whitespace) when parsing strings. (In such cases, the return value is not used.)
  Examples:

      string s = "123abc";
      string t = munch(s, "0123456789");
      assert(t == "123" && s == "abc");
      t = munch(s, "0123456789");
      assert(t == "" && s == "abc");

pure @safe S succ(S)(S s) if (isSomeString!S);
  Return string that is the 'successor' to s[]. If the rightmost character is a-zA-Z0-9, it is incremented within its case or digits. If it generates a carry, the process is repeated with the one to its immediate left.
  Examples:

      assert(succ("1") == "2");
      assert(succ("9") == "10");
      assert(succ("999") == "1000");
      assert(succ("zz99") == "aaa00");

C1[] tr(C1, C2, C3, C4 = immutable(char))(C1[] str, const(C2)[] from, const(C3)[] to, const(C4)[] modifiers = null);
  Replaces the characters in str which are in from with the the corresponding characters in to and returns the resulting string.
  tr is based on Posix's tr, though it doesn't do everything that the Posix utility does.
  Parameters:

  C1[] str | The original string.
  const(C2)[] from | The characters to replace.
  const(C3)[] to | The characters to replace with.
  const(C4)[] modifiers | String containing modifiers.

  Modifiers:

  Modifier | Description
  'c' | Complement the list of characters in from
  'd' | Removes matching characters with no corresponding replacement in to
  's' | Removes adjacent duplicates in the replaced characters

  If the modifier 'd' is present, then the number of characters in to may be only 0 or 1.

  If the modifier 'd' is not present, and to is empty, then to is taken to be the same as from.

  If the modifier 'd' is not present, and to is shorter than from, then to is extended by replicating the last character in to.

  Both from and to may contain ranges using the '-' character (e.g. "a-d" is synonymous with "abcd".) Neither accept a leading '^' as meaning the complement of the string (use the 'c' modifier for that).
pure @safe bool isNumeric(const(char)[] s, in bool bAllowSep = false);
  [in] string s can be formatted in the following ways:
  Integer Whole Number: (for byte, ubyte, short, ushort, int, uint, long, and ulong) ['+'|'-']digit(s)[U|L|UL]
  Examples:
  123, 123UL, 123L, +123U, -123L

  Floating-Point Number: (for float, double, real, ifloat, idouble, and ireal) ['+'|'-']digit(s)[.][digit(s)][[e-|e+]digit(s)][i|f|L|Li|fi]] or [nan|nani|inf|-inf]
  Examples:
  +123., -123.01, 123.3e-10f, 123.3e-10fi, 123.3e-10L

  (for cfloat, cdouble, and creal) ['+'|'-']digit(s)[.][digit(s)][[e-|e+]digit(s)][+] [digit(s)[.][digit(s)][[e-|e+]digit(s)][i|f|L|Li|fi]] or [nan|nani|nan+nani|inf|-inf]
  Examples:
  nan, -123e-1+456.9e-10Li, +123e+10+456i, 123+456

  [in] bool bAllowSep False by default, but when set to true it will accept the separator characters ',' and '_' within the string, but these characters should be stripped from the string before using any of the conversion functions like toInt(), toFloat(), and etc else an error will occur.

  Also please note, that no spaces are allowed within the string anywhere whether it's a leading, trailing, or embedded space(s), thus they too must be stripped from the string before using this function, or any of the conversion functions.
pure nothrow @safe char[] soundex(const(char)[] string, char[] buffer = null);
  Soundex algorithm.
  The Soundex algorithm converts a word into 4 characters based on how the word sounds phonetically. The idea is that two spellings that sound alike will have the same Soundex value, which means that Soundex can be used for fuzzy matching of names.
  Parameters:

  const(char)[] string | String to convert to Soundex representation.
  char[] buffer | Optional 4 char array to put the resulting Soundex characters into. If null, the return value buffer will be allocated on the heap.

  Returns:
  The four character array with the Soundex result in it. Returns null if there is no Soundex representation for the string.
  See Also:
  Wikipedia, The Soundex Indexing System
  Bugs:
  Only works well with English names. There are other arguably better Soundex algorithms, but this one is the standard one.
pure @safe string[string] abbrev(string[] values);
  Construct an associative array consisting of all abbreviations that uniquely map to the strings in values.
  This is useful in cases where the user is expected to type in one of a known set of strings, and the program will helpfully autocomplete the string once sufficient characters have been entered that uniquely identify it.

  Example:

      import std.stdio;
      import std.string;

      void main()
      {
         static string[] list = [ "food", "foxy" ];

         auto abbrevs = std.string.abbrev(list);

         foreach (key, value; abbrevs)
         {
            writefln("%s => %s", key, value);
         }
      }

  produces the output:

       fox => foxy
       food => food
       foxy => foxy
       foo => food

pure @nogc @safe size_t column(S)(S str, in size_t tabsize = 8) if (isSomeString!S);
  Compute column number at the end of the printed form of the string, assuming the string starts in the leftmost column, which is numbered starting from 0.
  Tab characters are expanded into enough spaces to bring the column number to the next multiple of tabsize, and carriage returns and newlines reset the running column number back to 0.
  Examples:

      assert(column("1234 ") == 5);

      // Tab stops are set at 8 spaces by default; tab characters insert enough
      // spaces to bring the column position to the next multiple of 8.
      assert(column("\t") == 8);
      assert(column("1\t") == 8);
      assert(column("\t1") == 9);
      assert(column("123\t") == 8);

      // Other tab widths are possible by specifying it explicitly:
      assert(column("\t", 4) == 4);
      assert(column("1\t", 4) == 4);
      assert(column("\t1", 4) == 5);
      assert(column("123\t", 4) == 4);

      // Newlines and carriage returns reset the column number.
      assert(column("abc\n") == 0);
      assert(column("abc\n1") == 1);
      assert(column("abcdefg\r1234") == 4);

pure @safe S wrap(S)(S s, in size_t columns = 80, S firstindent = null, S indent = null, in size_t tabsize = 8) if (isSomeString!S);
  Wrap text into a paragraph.
  The input text string s is formed into a paragraph by breaking it up into a sequence of lines, delineated by \n, such that the number of columns is not exceeded on each line. The last line is terminated with a \n.
  Parameters:

  S s | text string to be wrapped
  size_t columns | maximum number of columns in the paragraph
  S firstindent | string used to indent first line of the paragraph
  S indent | string to use to indent following lines of the paragraph
  size_t tabsize | column spacing of tabs

  Returns:
  The resulting paragraph.
pure @safe S outdent(S)(S str) if (isSomeString!S);
pure @safe S[] outdent(S)(S[] lines) if (isSomeString!S);
  Removes indentation from a multi-line string or an array of single-line strings.
  This uniformly outdents the text as much as possible. Whitespace-only lines are always converted to blank lines.

  A StringException will be thrown if inconsistent indentation prevents the input from being outdented.

  Works at compile-time.
  Examples:

      enum pretty = q{
         import std.stdio;
         void main() {
             writeln("Hello");
         }
      }.outdent();

      enum ugly = q{
      import std.stdio;
      void main() {
      writeln("Hello");
      }
      };

      assert(pretty == ugly);

pure auto assumeUTF(T)(T[] arr) if (staticIndexOf!(Unqual!T, ubyte, ushort, uint) != -1);
  Assume the given array of integers arr is a well-formed UTF string and return it typed as a UTF string.
  ubyte becomes char, ushort becomes wchar and uint becomes dchar. Type qualifiers are preserved.
  See Also:
  representation
  Examples:

      string a = "Hölo World";
      immutable(ubyte)[] b = a.representation;
      string c = b.assumeUTF;

      assert(a == c);

Copyright Digital Mars 2007-. | Page generated by Ddoc on Tue Mar 24 14:30:33 2015