Represents a sequence of unicode characters.
Strings are useful for holding data that can be represented in text form.
Strings are created using either single or double quotes.
string1 = "A string value" string2 = "This is also a string value"
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
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."
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() method formats according to a format specifier and returns the resulting string.
name = "Soma" age = 3 message = "%s is %s years old.".format(name, age) // expected value: "Soma is 3 years old."
|a string value|
endsWith() method determines if the given string ends with the given value.
result = "This is my name".endsWith("name") // expected value: true
length() method returns the length of the given string.
length = "Ghost".length() // expected value: 5
replace() method replaces a given string within the string.
replaced = "Ghost 0.x".replace("0.x", "1.x") // expected value: "Ghost 1.x"
split() method splits a string into a list by the given delimiter.
segments = "one, two, three".split(", ") // expected value: ["one", "two", "three"]
startsWith() method determines if the given string begins wih the given value.
result = "This is my name".startsWith("This") // expected value: true
toLowerCase() method converts the given string to lowercase.
value = "GHOST".toLowerCase() // expected value: "ghost"
toUpperCase() method converts the given string to uppercase.
value = "ghost".toUpperCase() // expected value: "GHOST"
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 = "Ghost".toString() // expected value: "Ghost"
toNumber() method converts the given string to a number if valid.
value = "3.14" // expected value: 3.14
trim() method trims the given string.
value = " Ghost ".trim() // expected value: "Ghost"
trimEnd() method trims the end of the given string.
value = " Ghost ".trimEnd() // expected value: " Ghost"
trimStart() method trims the start of the given string.
value = " Ghost ".trimStart() // expected value: "Ghost "