🧵
Strings
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."
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"]
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."
Formatter | Description |
---|---|
%s | a string value |
The
endsWith()
method determines if the given string ends with the given value.result = "This is my name".endsWith("name")
// expected value: true
The
length()
method returns the length of the given string.length = "Zaid".length()
// expected value: 4
The
replace()
method replaces a given string within the string.replaced = "Zaid 0.x".replace("0.x", "1.x")
// expected value: "Zaid 1.x"
The
split()
method splits a string into a list by the given delimiter.segments = "one, two, three".split(", ")
// expected value: ["one", "two", "three"]
The
startsWith()
method determines if the given string begins wih the given value.result = "This is my name".startsWith("This")
// expected value: true
The
toLowerCase()
method converts the given string to lowercase.value = "ZAID".toLowerCase()
// expected value: "zaid"
The
toUpperCase()
method converts the given string to uppercase.value = "zaid".toUpperCase(dad)
// expected value: "ZAID"
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"
The
toNumber()
method converts the given string to a number if valid.value = "3.14".toNumber()
// expected value: 3.14
The
trim()
method trims the given string.value = " Zaid ".trim()
// expected value: "Zaid"
The
trimEnd()
method trims the end of the given string.value = " Zaid ".trimEnd()
// expected value: " Zaid"
The
trimStart()
method trims the start of the given string.value = " Zaid ".trimStart()
// expected value: "Zaid "
Last modified 6mo ago