String Prototype

The string prototype ($str_proto) defines verbs that perform operations on the receiver, which will be of type STR. All verbs are of the general form:

any string:verb(...)

Where str may be a string literal or an expression returning a string value.

index rindex

INT str1:index(STR str2 [, ANY case_matters])
INT str1:rindex(STR str2 [, ANY case_matters])

Returns the index of the first character of the first/last occurrence of str2 in str1, or zero if str2 does not occur in str1 at all. By default the search for an occurrence of str2 is done while ignoring upper/lower-case distinctions. If case_matters is provided and true, then case is treated as significant in all comparisons.

"foobar":index("o")        => 2
"foobar":rindex("o")       => 3

match rmatch

LIST subject:match(STR pattern [, ANY case_matters])
LIST subject:rmatch(STR pattern [, ANY case_matters])

Searches for the first/last occurrence of the regular expression pattern in the string subject. Returns a list containing information about the match, or the empty list if no match is found. By default, the search ignores upper/lower-case distinctions. If case_matters is provided and true, then case is treated as significant in all comparisons.

The list that match()/rmatch() returns contains the details about the match made. The list is in the form:

{start, end, replacements, subject, pattern}

Examples:

"foo":match("^f*o$")        => {}
"foo":match("^fo*$")        => {1, 3, {{0, -1}, ...}, "foo", "^fo*$"}
"foobar":match("o*b")       => {2, 4, {{0, -1}, ...}, "foobar", "o*b"}
"foobar":rmatch("o*b")      => {4, 4, {{0, -1}, ...}, "foobar", "o*b"}

split explode

LIST string:split([STR delimiter])

Splits the string into a list of substrings based on the supplied delimiter. If delimiter is not specified it defaults to a space.

"one two three":split()        => {"one", "two", "three"}
"123-456-7890":split("-")      => {"123", "456", "7890"}

trim triml trimr

STR string:triml([STR pattern])
STR string:trimr([STR pattern])
STR string:trim([STR pattern])

Trims leading/trailing/both characters from the string. If pattern is not specified it defaults to a space.

"  foo  ":triml()        => "foo  "
"  foo  ":trimr()        => "  foo"
"  foo  ":trim()         => "foo"

strsub

STR string:strsub(STR from, STR to [, ANY case_matters])
STR string:strsub(MAP substitutions [, ANY case_matters])
STR string:strsub(LIST substitutions [, ANY case_matters])

Performs the specified substitutions on string. By default, comparisons ignore upper/lower-case distinctions. If case_matters is provided and true, then case is treated as significant in all comparisons.

"%n is a fink.":strsub("%n", "Fred")                    => "Fred is a fink."
"%n is a %m.":strsub(["%n" -> "Fred", "%m" -> "fink"])  => "Fred is a fink."
"%n is a %m.":strsub({{"%n", "Fred"}, {"%m", "fink"}})  => "Fred is a fink."

strtr

STR string:strtr(STR from, STR to [, ANY case_matters])

Returns a copy of string with the characters in from replaced by the corresponding characters in to. By default, translation ignores upper/lower-case distinctions. If case_matters is provided and true, then case is treated as significant in all translations.

"hello":strtr("el", "ip")      => "hippo"

repeat

STR string:repeat(INT length)

Returns a string with string repeated length times.

"123":repeat(3)     => "123123123"

fill space

STR string:fill(INT length)

Returns a string of length length made up of repetitions of string.

"123":fill(5)       => "12312"

right

STR string:right(INT length [, STR fill])

Returns a string of length length containing the right-justified string string. The optional argument fill specifies the fill/padding character and defaults to a space.

"foo":right(10)     => "       foo"

left

STR string:left(INT length [, STR fill])

Returns a string of length length containing the left-justified string string. The optional argument fill specifies the fill/padding character and defaults to a space.

"foo":left(10)     => "foo       "

center centre

STR string:center(INT length [, STR fill])

Returns a string of length length containing the centered string string. The optional argument fill specifies the fill/padding character and defaults to a space.

"foo":center(10)     => "   foo    "

to_upper*case

STR string:to_uppercase()

Returns a copy of string with all lower-case characters replaced by their upper-case counterparts.

"Foo":to_uppercase()     => "FOO"

to_lower*case

STR string:to_lowercase()

Returns a copy of string with all upper-case characters replaced by their lower-case counterparts.

"Foo":to_lowercase()     => "foo"

capitalize

STR string:capitalize()

Returns a copy of string with the first character upper-cased and the remaining characters lower-cased.

"eL1t3":capitalize()     => "El1t3"

strip*_characters

STR string:strip_characters(STR other)

Returns a copy of string with the characters in other removed.

"foo bar baz":strip_characters("fb")     => "oo ar az"

is_empty

INT string:is_empty()

Returns true if string is the empty string.

"  ":is_empty()     => 0
"":is_empty()       => 1

is_blank

INT string:is_blank()

Returns true if string is composed entirely of whitespace.

"  ":is_blank()     => 1
"":is_blank()       => 1

partition

LIST string:partition(STR separator)

Finds the first occurrence of separator in string and returns a list consisting of the part of the string before the separator, the separator, and the part of the string after the separator. If there is no match, the verb returns the original string and two empty strings.

"foo/bar/baz":partition("/")     => {"foo", "/", "bar/baz"}
"foo/bar/baz":partition(" ")     => {"foo/bar/baz", "", ""}

rpartition

LIST string:rpartition(STR separator)

Finds the last occurrence of separator in string and returns a list consisting of the part of the string before the separator, the separator, and the part of the string after the separator. If there is no match, the verb returns two empty strings and the original string.

"foo/bar/baz":rpartition("/")    => {"foo/bar", "/", "baz"}
"foo/bar/baz":rpartition(" ")    => {"", "", "foo/bar/baz"}

reverse

STR string:reverse()

Returns a copy of string with the characters reversed.

"foobar":reverse()     => "raboof"

squeeze

STR string:squeeze(STR other)

Returns a copy of string with runs of the characters in other replaced by a single character. If no argument is specified, all runs of the same characters are replaced by a single character.

"foo bar baz":squeeze("oa")     => "fo bar baz"
"yellow moon":squeeze()         => "yelow mon"

next

STR string:next()

Returns the successor to string. The successor is created by incrementing the rightmost alphanumeric through the range [0-9A-Za-z]. If the operation overflows, the process repeats with the next rightmost character until all characters in the string have been incremented. If the leftmost character overflows, the verb raises E_INVARG.

"abcd":next()        => "abce"
"THX1138":next()     => "THX1139"
"<<koala>>":next()   => "<<koalb>>"
"1999zzz":next()     => "199A000"
"zzz":next()         raises E_INVARG 

previous

STR string:previous()

Returns the predecessor to string. The predecessor is created by decrementing the rightmost alphanumeric through the ranges [0-9A-Za-z]. If the operation underflows, the process repeats with the next rightmost character until all characters in the string have been decremented. If the leftmost character underflows, the verb raises E_INVARG.

"abce":previous()        => "abcd"
"THX1139":previous()     => "THX1138"
"<<koalb>>":previous()   => "<<koala>>"
"199A000":previous()     => "1999zzz"
"000":previous()         raises E_INVARG 

hash

STR string:hash([STR algorithm])

Returns the hash of string computed with the specified algorithm. "sha256", "sha1" and "md5" are supported. If no algorithm is specified, "sha256" is assumed.

"foo":hash()         => "2C26B46B68FFC68FF99B453C1D30413413422D706483BFA0F98A5E886266E7AE"
"foo":hash("md5")    => "ACBD18DB4CC2F85CEDEF654FCCC4A4D8"

truncate ellipses

STR string:truncate([INT maximum], [STR omission])

Truncates the string to the specified maximum length. If truncated, the last characters will be replaced by omission. If maximum is not specified it defaults to 70 characters. if omission is not specified it defaults to "...".

"foobarbaz":truncate(6)            => "foo..."
"foobarbaz":truncate(6, "**")      => "foob**"
"foobarbaz":truncate()             => "foobarbaz"

encode_base64

STR string:encode_base64()

Base64 encodes the string.

"abc":encode_base64()    => "YWJj"

decode_base64

STR string:decode_base64()

Base64 decodes the string.

"YWJj":decode_base64()    => "abc"

escape_html_element_content

STR string:escape_html_element_content()

Escapes untrusted HTML element content. Note: this verb is typically not sufficient for escaping HTML attribute content, especially content used in JavaScript or CSS contexts, or in unquoted attributes. See the OWASP XSS Cheat Sheet for more information.

"<foo>&bar</foo>":escape_html_element_content()    => "&lt;foo&gt;&amp;bar&lt;&#x2F;foo&gt;"

escape_html_attribute_content

STR string:escape_html_attribute_content()

Escapes untrusted HTML attribute content. Note: this verb is often not sufficient for escaping HTML attribute content in JavaScript, CSS and URL contexts. See the OWASP XSS Cheat Sheet for more information.

"</ *&%}0\",_->":escape_html_attribute_content()    => "&lt;&#x2F;&#x20;&#x2A;&amp;&#x25;&#x7D;0&quot;&#x2C;&#x5F;&#x2D;&gt;"

unescape_html

STR string:unescape_html()

Unescapes HTML content. Intended as a compliment to escape_html_element_content() and escape_html_attribute_content().

"&lt;foo&gt;&amp;bar&lt;&#x2F;foo&gt;":unescape_html()                                    => "<foo>&bar</foo>"
"&lt;&#x2F;&#x20;&#x2A;&amp;&#x25;&#x7D;0&quot;&#x2C;&#x5F;&#x2D;&gt;":unescape_html()    => "</ *&%}0\",_->"

asc

INT char:asc()

Returns the index of the character chr in the ASCII character set.

",":asc()    => 44
"^":asc()    => 94