Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

The following operators are valid for strings.

decode()

The decode() operator converts a JSON string into the equivalent KRL data structure. For example:
a = "[3, 4, 5]";
c = a.decode(); // c = [3, 4, 5]

extract()

The extract() operator matches the string using a regular expression given as the sole argument and returns the specified capture groups as an array. For example:
foo = "I like cheese";
my_str = "This is a string";
a = my_str.extract(re/(is)/) // a = ["is"]
b = my_str.extract(re/(s.+).*(.ing)/) // b = ["s is a st","ring"]
c = my_str.extract(re/(boot)/) // c = []
d = foo.extract(re/like (\w+)/) // d = ["cheese"]
e = foo.extract(re/(e)/g) // e ["e","e","e","e"]
Note that if the regular expression does not contain at least one capture group, the resulting array will be empty.

lc()

The lc() operator returns the lowercase version of the original string. For example:
a = "Hello World";
c = a.lc() // c = "hello world"

match()

The match() operator takes a regular expression as its argument. The result is true if the regular expression matches the string and false otherwise. For example:
my_str = "This is a string";
a = my_str.match(re/is/) // a = true
b = my_str.match(re/mouse/) // b = false

replace()

The replace() operator takes two arguments: a regular expression and a string. The returned string is the original string with any match of the regular expression replaced by the second argument. You can use any captured values in the second, substitute string by naming them with $1, $2, and so on. The first captured value will be substituted for $1, the second for $2, and so on. For example:
my_str = "This is a string";
my_url = "http://www.amazon.com/gp/products/123456789/";
a = my_str.replace(re/is/,"ese"); // a = "These is a string"
b = my_str.replace(re/is/g,"ese"); // b = "These ese a string"
c = my_str.replace(re/this/,"That"); // c = "This is a string" (no change)
d = my_str.replace(re/this/i,"That"); // d = "That is a string"
e = my_url.replace(re#^http://(\[A-Za-z0-9.-\]+)/.*#,"$1");
// e = "www.amazon.com"
In the final example, I was careful to ensure that the regular expression matched the entire URL, so that the substitution of $1 resulted in just the domain name. This same result could be more easily achieved using extract()—although the result, e, would be an array:
e = my_url.extract(re#^http://(\[A-Za-z0-9.-\]+)/#);

split()

The split() operator takes a regular expression as its sole argument. The regular expression is used to split the original string into an array. For example:
a = "A;B;C"
c = a.split(re/;/) // c = ["A","B","C"]

sprintf()

The sprintf() operator can be applied to either a number or a string. The operator takes a formatting string as its sole argument. The formatting string follows the conventions for sprintf() established in other languages. Specifically, the KRL version of sprintf() follows the formatting conventions for Perl.3 For example:
a = 10
c = a.sprintf("< %d>") // c = "< 10>"

uc()

The uc() operator returns the uppercase version of the original string. For example:
a = "Hello World";
c = a.uc() // c = "HELLO WORLD"

  • No labels