String methods
You can find more info about the particular regex syntax used in the *_regex methods on this link.

.to_uppercase()

Return the same string in all uppercase characters.
1
string.to_uppercase() => String
2
3
// example
4
do val = "Where is Brian?"
5
do val.to_uppercase() // "WHERE IS BRIAN?"
Copied!

.to_lowercase()

Return the same string in all lowercase characters.
1
string.to_lowercase() => String
2
3
// example
4
do val = "Where is Brian?"
5
do val.to_lowercase() // "where is brian?"
Copied!

.capitalize()

Return the same string with the first letter in uppercase. The rest of the string remains unchanged.
1
string.capitalize() => String
2
3
// example
4
do val = "my name is John"
5
do val.capitalize() // "My name is John"
Copied!

.length()

Return the length of the target string.
1
string.length() => Integer
2
3
// example
4
do val = "Where is Brian?"
5
do val.length() // 15
Copied!

.contains(String), .contains_regex(String)

Return whether the string contains another string or expression.
1
haystack.contains(needle) => Boolean
2
haystack.contains_regex(needle) => Boolean
3
4
// example
5
do val = "Where is Brian?"
6
// does it contain any "r"?
7
do val.contains("r") // true
8
// does it contain the word "where"?
9
do val.contains("where") // false => no, because it is case sensitive
10
// does it contain any number?
11
do val.contains_regex("[0-9]") // true
Copied!

.starts_with(String), .starts_with_regex(String)

Return whether a string starts with another string or expression.
1
haystack.starts_with(needle) => Boolean
2
haystack.starts_with_regex(needle) => Boolean
3
4
// example
5
do val = "Where is Brian?"
6
// does it start with "r"?
7
do val.starts_with("r") // false
8
// does it start with any uppercase letter?
9
do val.starts_with_regex("[A-Z]") // true
Copied!

.ends_with(String), .ends_with_regex(String)

Return whether a string ends with another string or expression.
1
haystack.ends_with(needle) => Boolean
2
haystack.ends_with_regex(needle) => Boolean
3
4
// example
5
do val = "Where is Brian?"
6
// does it end with "r"?
7
do val.ends_with("r") // false
8
// does it end with any uppercase letter?
9
do val.ends_with_regex("[A-Z]") // false
Copied!

.match(String), .match_regex(String)

Return all the matches of the string or expression in the target string, or Null if none are found.
1
haystack.match(needle) => Array[String]
2
haystack.match_regex(needle) => Array[String]
3
4
// example
5
do val = "Where is Brian?"
6
// does it match with "r"?
7
do val.match("r") // ["r", "r"] => yes, twice!
8
// does it match with any uppercase letter?
9
do val.match_regex("[A-Z]") // ["W", "B"] => yes, and these are the letters!
Copied!
About _regex methods:
The \ (backslash) character has a special meaning. For technical reasons, in all strings, it must be properly escaped, by convention by adding another \ in front of itself, to avoid being interpreted as a special character. For example, if you mean to write the exact string "\n" you must in fact write \\n, otherwise \n will be interpreted as a line break.
This Python documentation explains why it especially matters in Regex syntax to escape backslashes: https://docs.python.org/2/howto/regex.html#the-backslash-plague
We follow this nomenclature for CSML Regex handling, so a single Regex backslash must be written as a "\\" string, and an escaped backslash (that behaves as a literal "\" string character) must in fact be escaped twice, once for being in a string, and once for being in a Regex: you have to write "\\\\" to result in the Regex syntax \\which in turn matches the literal "\" string.
In a future release of CSML we might introduce a "raw string" method to bypass this limitation.

.is_number(), .is_int(), .is_float()

Return whether the given string represents a numerical value, an int, a float.
1
string.is_number() => Boolean
2
3
// example
4
do val = "Where is Brian?"
5
do val.is_number() // false
6
7
do val = "42"
8
do val.is_number() // true
9
do val.is_int() // true
10
do val.is_float() // false
Copied!

.split(String)

Split a string by a given separator and return an array containing all elements in order. The separator can be a single or multiple characters. If the separator can not be found in the string, the returned array will only contain the original string.
1
string.split(String) => Array[String]
2
3
// example
4
do val = "this is a long string"
5
do val.split(" ") // ["this", "is", "a", "long", "string"]
6
do val.split("is") // ["th", " ", " a long string"]
7
do val.split("camembert") // ["this is a long string"]
Copied!

.slice(start, end) => String

Cut a string between the start and end characters. Some rules apply:
  • If end is not specified, all the characters after start are returned.
  • When specified, end must be ≥ start.
  • If any of the parameters is < 0, the count is made from the end of the string.
1
say "abcdefghijklmnop".slice(2, 4) // "cd"
2
say "abcdefghijklmnop".slice(7) // "hijklmnop"
3
say "abcdefghijklmnop".slice(-4) // "mnop"
4
say "abcdefghijklmnop".slice(-4, 14) // "mn"
5
6
say "abcdefghijklmnop".slice(-4, 3) // Error
7
say "abcdefghijklmnop".slice(2, 1) // Error
Copied!
Last modified 7mo ago