Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

String Library

The string library provides functionality for manipulating strings. Most of these functions are accessible as methods of string objects, as if they were classes. Only one function must be accessed through the global 'string' namespace.

Remember that strings in MiniD are immutable. Therefore these functions never operate on the instance on which they were called. They will always return new strings distinct from the original string.

string.join(arr, sep)

Given an array of strings arr, and a separator sep, this function will join them sequentially with sep in between them. So calling string.join(["apple", "banana", "orange"], ".") will yield the single string "apple.banana.orange". If the array is empty, an empty string will be returned. All the elements of the array must be strings, or an exception will be thrown.

Methods

These are all called as "s.methodname()", where "s" is any string object.

s.toInt(base = 10)

Converts the string into an integer. If the string does not follow the format of an integer, an exception will be thrown. The optional base parameter defaults to 10, but you can use any base between 2 and 36 inclusive.

s.toFloat()

Converts the string into a float. If the string does not follow the format of a float, an exception will be thrown.

s.compare(other)

Compares the string to the string other, and returns an integer. If s is less than (alphabetically) other, the return is negative; if they are the same, the return is 0; and otherwise, the return is positive.

s.icompare(other)

Compares the string to the string other, and returns an integer. This function ignores case, so "foo", "Foo", and "fOO" will all compare the same. The return values are the same as .compare().

s.find(sub)

Searches for an occurence of sub in the string. sub can be either a string or a single character. The search starts from the first character of the string and goes right. If sub is found, this function returns the integer index of the occurrence in the string, with 0 meaning the first character. Otherwise, -1 is returned.

s.ifind(sub)

Searches for an occurence of sub in the string. sub can be either a string or a single character. This search is not case-sensitive. The search starts from the first character of the string and goes right. If sub is found, this function returns the integer index of the occurrence in the string, with 0 meaning the first character. Otherwise, -1 is returned.

s.rfind(sub)

Searches for an occurence of sub in the string. sub can be either a string or a single character. The search starts from the last character of the string and goes left. If sub is found, this function returns the integer index of the occurrence in the string, with 0 meaning the first character. Otherwise, -1 is returned.

s.irfind(sub)

Searches for an occurence of sub in the string. sub can be either a string or a single character. This search is not case-sensitive. The search starts from the last character of the string and goes left. If sub is found, this function returns the integer index of the occurrence in the string, with 0 meaning the first character. Otherwise, -1 is returned.

s.toLower()

Returns a new string with any uppercase letters converted to lowercase. Non-uppercase letters and non-letters are not affected.

s.toUpper()

Returns a new string with any lowercase letters converted to uppercase. Non-lowercase letters and non-letters are not affected.

s.repeat(n)

Returns a string which is the concatenation of n instances of the string. So "hello".repeat(3) will return "hellohellohello". n must be an integer and must be greater than or equal to 0.

s.split([delim])

Splits a string into pieces and returns an array of strings. If no parameters are given, the splitting occurs at whitespace (spaces, tabs, newlines etc.) and all the whitespace is stripped from the split pieces. If the delim parameter is given, it must be a string and it specifies where the string should be split. This can be used as the inverse of string.join.

s.splitLines()

This will split the string at any newline characters ('\n', '\r', or '\r\n'). Other whitespace is preserved, and empty lines are preserved. This returns an array of strings, each of which holds one line of text.

s.strip()

Strips any whitespace from the beginning and end of the string.

s.lstrip()

Strips any whitespace from the beginning of the string.

s.rstrip()

Strips any whitespace from the end of the string.

s.replace(from, to)

Replaces any occurrences in s of the string from with the string to.

s.opApply([direction])

This is an iterator bootstrap function which allows you to iterate over a string with a foreach loop.

Since this is a method of string objects, you shouldn't have to call this function directly. Simply put the string as the container of the foreach loop:

foreach(i, v; "hello")
	writefln("string[", i, "] = ", v);

foreach(i, v; "hello", "reverse")
	writefln("string[", i, "] = ", v);

As this example shows, if you pass "reverse" to the opApply function, either directly or as the second part of the foreach container, the iteration will go in reverse, starting at the end of the string.

s.startsWith(string)

Returns a bool of whether or not s starts with the substring string. This is case-sensitive.

s.endsWith(string)

Returns a bool of whether or not s ends with the substring string. This is case-sensitive.

s.istartsWith(string)

Returns a bool of whether or not s starts with the substring string. This is case-insensitive.

s.iendsWith(string)

Returns a bool of whether or not s ends with the substring string. This is case-insensitive.