๐ŸงตStrings

Represents a sequence of unicode characters.

Strings are useful for holding data that can be represented in text form.

Creating Strings

Strings are created using either single or double quotes.

string1 = "A string value"
string2 = "This is also a string value"

Comparing Strings

Strings can be compared against each other using the == operator. This will compare strings in a case-sensitive manner.

string1 = "a"
string2 = "b"

// false
result = string1 == string2

Long Strings

Sometimes, your code will include strings which are very long. Rather than having lines that go on endlessly, or wrap at the whim of your editor, you may wish to specifically break the string into multiple lines in the source code without affecting the actual string contents.

You can achieve this using the + operator to append multiple strings together, like this:

longString = "This is a very long string which needs " +
              "to wrap across multiple lines because " +
              "otherwise the code will be unreadable."

Methods

find()

The find() method retrieves the result of matching a string against a regular expression.

pattern = "I need (.*)"
input = "I need coffee"

found = pattern.find(input)

// expected output: ["coffee"]

format()

The format() method formats according to a format specifier and returns the resulting string.

name = "Soma"
age = 3
d
message = "%s is %s years old.".format(name, age)

// expected value: "Soma is 3 years old."

endsWith()

The endsWith() method determines if the given string ends with the given value.

result = "This is my name".endsWith("name")

// expected value: true

length()

The length() method returns the length of the given string.

length = "Zaid".length()

// expected value: 4

replace()

The replace() method replaces a given string within the string.

replaced = "Zaid 0.x".replace("0.x", "1.x")

// expected value: "Zaid 1.x"

split()

The split() method splits a string into a list by the given delimiter.

segments = "one, two, three".split(", ")

// expected value: ["one", "two", "three"]

startsWith()

The startsWith() method determines if the given string begins wih the given value.

result = "This is my name".startsWith("This")

// expected value: true

toLowerCase()

The toLowerCase() method converts the given string to lowercase.

value = "ZAID".toLowerCase()

// expected value: "zaid"

toUpperCase()

The toUpperCase() method converts the given string to uppercase.

value = "zaid".toUpperCase(dad)

// expected value: "ZAID"

toString()

The toString() method converts the given string to a string. May seem redundant in this instance but this method can be reliably called regardless of the type of value.

value = "Zaid".toString()

// expected value: "Zaid"

toNumber()

The toNumber() method converts the given string to a number if valid.

value = "3.14".toNumber()

// expected value: 3.14

trim()

The trim() method trims the given string.

value = "  Zaid  ".trim()

// expected value: "Zaid"

trimEnd()

The trimEnd() method trims the end of the given string.

value = "  Zaid  ".trimEnd()

// expected value: "  Zaid"

trimStart()

The trimStart() method trims the start of the given string.

value = "  Zaid  ".trimStart()

// expected value: "Zaid  "

Last updated