View source code
Display the source code in std/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.

Module std.string

String handling functions.

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.

The following functions are publicly imported:

Module Functions
    Publicly imported functions
std.algorithm cmp count endsWith startsWith
std.array join replace replaceInPlace split empty
std.format format sformat
std.uni icmp toLower toLowerInPlace toUpper toUpperInPlace

There is a rich set of functions for string handling defined in other modules. 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.

See Also

Functions

NameDescription
abbrev(values) Construct an associative array consisting of all abbreviations that uniquely map to the strings in values.
assumeUTF(arr) Assume the given array of integers arr is a well-formed UTF string and return it typed as a UTF string.
capitalize(input) Capitalize the first character of s and convert the rest of s to lowercase.
center(s, width, fillChar) 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.
centerJustifier(r, width, fillChar) Center justify r in a field width characters wide. fillChar is the character that will be used to fill up the space in the field that r doesn't fill.
chomp(str) 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.
chompPrefix(str, delimiter) If str starts with delimiter, then the part of str following delimiter is returned. If str does not start with
chop(str) 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.
column(str, tabsize) 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.
countchars(s, pattern) This function is deprecated and will be removed May 2018. Please use the functions in std.regex and std.algorithm instead. If you still need this function, it will be available in undeaD.
detab(s, tabSize) Replace each tab character in s with the number of spaces necessary to align the following character at the next tab stop.
detabber(r, tabSize) Replace each tab character in r with the number of spaces necessary to align the following character at the next tab stop.
entab(s, tabSize) Replaces spaces in s with the optimal number of tabs. All spaces and tabs at the end of a line are removed.
entabber(r, tabSize) Replaces spaces in range r with the optimal number of tabs. All spaces and tabs at the end of a line are removed.
fromStringz(cString)
indexOf(s, c, cs) Searches for character in range.
indexOf(s, sub, cs) Searches for substring in s.
indexOfAny(haystack, needles, cs) Returns the index of the first occurrence of any of the elements in needles in haystack. If no element of needles is found, then -1 is returned. The startIdx slices haystack 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 UTFException may be thrown.
indexOfNeither(haystack, needles, cs) Returns the index of the first occurrence of any character not an elements in needles in haystack. If all element of haystack are element of needles -1 is returned.
inPattern(c, pattern) This function is deprecated and will be removed May 2018. Please use the functions in std.regex and std.algorithm instead. If you still need this function, it will be available in undeaD.
inPattern(c, patterns) This function is deprecated and will be removed May 2018. Please use the functions in std.regex and std.algorithm instead. If you still need this function, it will be available in undeaD.
isNumeric(s, bAllowSep) Takes a string s and determines if it represents a number. This function also takes an optional parameter, bAllowSep, which 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 to!int(), to!float(), and etc else an error will occur.
lastIndexOf(s, c, cs)
lastIndexOf(s, sub, cs)
lastIndexOfAny(haystack, needles, cs) Returns the index of the last occurrence of any of the elements in needles in haystack. If no element of needles is found, then -1 is returned. The stopIdx slices haystack in the following way s[0 .. stopIdx]. stopIdx represents a codeunit index in haystack. If the sequence ending at startIdx does not represent a well formed codepoint, then a UTFException may be thrown.
lastIndexOfNeither(haystack, needles, cs) 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.
leftJustifier(r, width, fillChar) 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.
leftJustify(s, width, fillChar) 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.
lineSplitter(r) Split an array or slicable range of characters into a range of lines using '\r', '\n', '\v', '\f', "\r\n", lineSep, paraSep and '\u0085' (NEL) as delimiters. If keepTerm is set to Yes.keepTerminator, then the delimiter is included in the slices returned.
makeTrans(from, to) Do same thing as makeTransTable but allocate the translation table on the GC heap.
makeTransTable(from, to) Construct 256 character translation table, where characters in from[] are replaced by corresponding characters in to[].
munch(s, pattern) This function is deprecated and will be removed May 2018. Please use the functions in std.regex and std.algorithm instead. If you still need this function, it will be available in undeaD.
outdent(str) Removes one level of indentation from a multi-line string.
outdent(lines) Removes one level of indentation from an array of single-line strings.
removechars(s, pattern) This function is deprecated and will be removed May 2018. Please use the functions in std.regex and std.algorithm instead. If you still need this function, it will be available in undeaD.
representation(s) 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.
rightJustifier(r, width, fillChar) 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.
rightJustify(s, width, fillChar) 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.
soundex(str, buffer) Like soundexer, but with different parameters and return value.
soundexer(str) Soundex algorithm.
splitLines(s, keepTerm) Split s into an array of lines according to the unicode standard using '\r', '\n', "\r\n", lineSep, paraSep, U+0085 (NEL), '\v' and '\f' as delimiters. If keepTerm is set to KeepTerminator.yes, then the delimiter is included in the strings returned.
squeeze(s, pattern) This function is deprecated and will be removed May 2018. Please use the functions in std.regex and std.algorithm instead. If you still need this function, it will be available in undeaD.
strip(str) Strips both leading and trailing whitespace (as defined by isWhite) or as specified in the second argument.
stripLeft(input) Strips leading whitespace (as defined by isWhite) or as specified in the second argument.
stripRight(str) Strips trailing whitespace (as defined by isWhite) or as specified in the second argument.
succ(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.
toStringz(s)
tr(str, from, to, modifiers) Replaces the characters in str which are in from with the the corresponding characters in to and returns the resulting string.
translate(str, transTable, toRemove) 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.
translate(str, transTable, toRemove, buffer) This is an overload of translate which takes an existing buffer to write the contents to.
translate(str, transTable, toRemove) 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.
translate(str, transTable, toRemove, buffer) This is an ASCII-only overload of translate which takes an existing buffer to write the contents to.
wrap(s, columns, firstindent, indent, tabsize) Wrap text into a paragraph.

Classes

NameDescription
StringException Exception thrown on errors in std.string functions.

Aliases

NameTypeDescription
CaseSensitive Flag!("caseSensitive") Flag indicating whether a search is case-sensitive.
KeepTerminator Flag!("keepTerminator") Split s into an array of lines according to the unicode standard using '\r', '\n', "\r\n", lineSep, paraSep, U+0085 (NEL), '\v' and '\f' as delimiters. If keepTerm is set to KeepTerminator.yes, then the delimiter is included in the strings returned.

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis, and David L. 'SpottedTiger' Davis

License

Boost License 1.0.