Strings

Words, letters, strings!

Basics

Strings are a representation of characters in a specific order that have a given length. Strings are an immutable data type, and therefore cannot be modified directly, so, when "modifying" a string, a new string is created.

String Definitions

To define a string, just put text between single or double quotes

Example

example_string_definition.cb
var string = "hello, world!";
println(string); // Prints "hello, world!"

String Concatenation

Putting two or more strings together is called concatenation. To concatenate strings, use the + operator.

Example

example_string_concat.cb
var string1 = "hello ";
var string2 = "world";
‚Äč
string1 + string2; // "hello world"

String Methods

.split(delim)

The split method splits a string into a list of strings based on a delimiter.

Signature:

list string.split(string)

Example:

example_string_split.cb
"hello world".split(" "); // ['hello', 'world']

.lower()

The lower method converts a string to lower case.

Signature:

string string.lower()

Example:

example_string_lower.cb
"CABOOSE".lower(); // caboose
"CaBoOsE".lower(); // caboose

.upper()

The upper method converts a string to upper case.

Signature:

string string.upper()

Example:

example_string_upper.cb
"caboose".upper(); // CABOOSE
"CaBoOsE".upper(); // CABOOSE

.startsWith(start)

The startsWith method checks whether a string starts with another string.

Signature:

bool string.startsWith(string)

Example:

example_string_startswith.cb
"caboose".startsWith("c"); // true
"caboose".startsWith("e"); // false

.endsWith(end)

The endsWith method checks if a string ends with another string.

Signature:

bool string.endsWith(string)

Example:

example_string_endswith.cb
"caboose".endsWith("c"); // false
"caboose".endsWith("e"); // true

.replace(old, new)

The replace method replaces all occurrences of the value of old with the value of new.

Signature:

string string.replace(string, string)

Example:

example_string_replace.cb
"Caboose is okay".replace("okay", "awesome"); // "Caboose is awesome"

.contains(word)

The contains method checks if a string contains another string.

Signature:

bool string.contains(string)

Example:

"Caboose is awesome".contains("Caboose"); // true

.rightStrip()

The rightStrip method strips all whitespace from the end of a string.

Signature:

string string.rightStrip()

Example:

example_string_rightstrip.cb
"Caboose ".rightStrip(); // "Caboose"

.leftStrip()

The leftStrip method strips all whitespace from the beginning of a string.

Signature:

string string.leftStrip()

Example:

example_string_leftstrip.cb
" Caboose".leftStrip(); // "Caboose"

.strip()

The strip method strips all whitespace from the beginning and end of a string.

Signature:

string string.strip()

Example:

example_string_strip.cb
" Caboose ".strip(); // "Caboose"

.find(needle, skip)

The find method finds the index of a given substring. Skip is an optional parameter which can be passed to skip the first n amount of appearances of the given substring.

Signature:

number string.find(string, number)

Example:

example_string_find.cb
"Hello, how are you?".find("how"); // 7
"hello something hello".find("hello", 2); // 16 -- skips the first occurrance of "hello"

.format(values...)

Signature:

string string.format(any...)

Example:

example_string_format.cb
"Hello {}".format("Rail"); // "Hello Rail"
"{} {} {} {}".format(10, "hi", [10, 20], {"test": 10}) // '10 hi [10, 20] {"test": 10}'