Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
285 lines (199 sloc) 5.14 KB
description
Words, letters, strings!

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

{% code-tabs %} {% code-tabs-item title="example_string_definition.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

String Concatenation

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

Example

{% code-tabs %} {% code-tabs-item title="example_string_concat.cb" %}

var string1 = "hello ";
var string2 = "world";

string1 + string2; // "hello world"

{% endcode-tabs-item %} {% endcode-tabs %}

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:

{% code-tabs %} {% code-tabs-item title="example_string_split.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.lower()

The lower method converts a string to lower case.

Signature:

string string.lower()

Example:

{% code-tabs %} {% code-tabs-item title="example_string_lower.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.upper()

The upper method converts a string to upper case.

Signature:

string string.upper()

Example:

{% code-tabs %} {% code-tabs-item title="example_string_upper.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.startsWith(start)

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

Signature:

bool string.startsWith(string)

Example:

{% code-tabs %} {% code-tabs-item title="example_string_startswith.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.endsWith(end)

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

Signature:

bool string.endsWith(string)

Example:

{% code-tabs %} {% code-tabs-item title="example_string_endswith.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.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:

{% code-tabs %} {% code-tabs-item title="example_string_replace.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.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:

{% code-tabs %} {% code-tabs-item title="example_string_rightstrip.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.leftStrip()

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

Signature:

string string.leftStrip()

Example:

{% code-tabs %} {% code-tabs-item title="example_string_leftstrip.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.strip()

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

Signature:

string string.strip()

Example:

{% code-tabs %} {% code-tabs-item title="example_string_strip.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.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:

{% code-tabs %} {% code-tabs-item title="example_string_find.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

.format(values...)

Signature:

string string.format(any...)

Example:

{% code-tabs %} {% code-tabs-item title="example_string_format.cb" %}

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

{% endcode-tabs-item %} {% endcode-tabs %}

You can’t perform that action at this time.