String Library
The string library provides functionality for manipulating strings. These functions are accessible as methods of string objects, as if they were classes.
Remember that strings in MiniD are immutable. Therefore these functions never operate on the object on which they were called. They will always return new strings distinct from the original string.
Members
- string.joinArray(arr: array, sep: string = "")
- Methods
- s.join(vararg)
- s.toInt(base: int = 10)
- s.toFloat()
- s.compare(other: string)
- s.icompare(other: string)
- s.find(sub: string|char, start: int = 0)
- s.ifind(sub: string|char, start: int = 0)
- s.rfind(sub: string|char, start: int = #s)
- s.irfind(sub: string|char, start: int = #s)
- s.toLower()
- s.toUpper()
- s.repeat(n: int)
- s.reverse()
- s.split([delim: string])
- s.splitLines()
- s.strip()
- s.lstrip()
- s.rstrip()
- s.replace(from: string, to: string)
- s.opApply([reverse: string])
- s.startsWith(string: string)
- s.endsWith(string: string)
- s.istartsWith(string: string)
- s.iendsWith(string: string)
string.joinArray(arr: array, sep: string = "")
Join all the strings and characters in arr into a single string, using sep as the separator between each item. sep defaults to the empty string, in which case there will be no separator between items, and it will work like a simple mass concatenation. Returns the resultant string.
Methods
These are all called as "s.methodname()", where "s" is any string object.
s.join(vararg)
This joins together the arguments using s as the separator. The arguments must all be characters or strings. If s is the empty string, this just concatenates all the arguments together. If there are 0 arguments, returns the empty string. If there is 1 argument, returns that argument as a string (so a single character will be converted to a string). Otherwise, returns the arguments joined sequentially with the separator (s) between each pair of arguments. So "".".join("apple", "banana", "orange")" will yield the string "apple.banana.orange".
s.toInt(base: int = 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: string)
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: string)
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: string|char, start: int = 0)
Searches for an occurence of sub in the string. sub can be either a string or a single character. The search starts from start (which defaults to the first character) 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, #s is returned.
s.ifind(sub: string|char, start: int = 0)
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 start (which defaults to the first character) 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, #s is returned.
s.rfind(sub: string|char, start: int = #s)
Searches for an occurence of sub in the string. sub can be either a string or a single character. The search starts with the character at start - 1 (which defaults to the last character) and goes left. start is not included in the search so you can use the result of this function as the start parameter to successive calls. If sub is found, this function returns the integer index of the occurrence in the string, with 0 meaning the first character. Otherwise, #s is returned.
s.irfind(sub: string|char, start: int = #s)
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 with the character at start - 1 (which defaults to the last character) and goes left. start is not included in the search so you can use the result of this function as the start parameter to successive calls. If sub is found, this function returns the integer index of the occurrence in the string, with 0 meaning the first character. Otherwise, #s 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: int)
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.reverse()
Returns a string which is the reversal of this string.
s.split([delim: string])
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: string, to: string)
Replaces any occurrences in s of the string from with the string to.
s.opApply([reverse: string])
This function allows you to iterate over the characters of a string with a foreach loop.
foreach(i, v; "hello") writeln("string[", i, "] = ", v) foreach(i, v; "hello", "reverse") writeln("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: string)
Returns a bool of whether or not s starts with the substring string. This is case-sensitive.
s.endsWith(string: string)
Returns a bool of whether or not s ends with the substring string. This is case-sensitive.
s.istartsWith(string: string)
Returns a bool of whether or not s starts with the substring string. This is case-insensitive.
s.iendsWith(string: string)
Returns a bool of whether or not s ends with the substring string. This is case-insensitive.
