The String class represents immutable byte sequences, commonly used for text encoded as UTF-8. Its methods provide searching, slicing, splitting, case conversion, and other string operations.

String.len

attribute string

Signature

String:len

Description

The attribute len allows to receive the byte length of a Tea string.

Example

const s = "Hello, world!"
print(s.len)  // 13

print("Exploding Kittens!".len) // 19

String.upper

method string

Signature

function String:upper()

Description

The upper method produces a new Tea string copy of the original string with all its characters uppercase.

Example

var str = "Hello, world!"
str = my_string.upper()
print(str)  // "HELLO, WORLD!"

String.lower

method string

Signature

function String:lower()

Description

The lower method produces a new Tea string copy of the original string with all its characters lowercase.

Example

var str = "Hello, world!"
str = my_string.lower()
print(str)  // "hello, world!"

String.reverse

method string

Signature

function String:reverse()

Description

The reverse method produces a new Tea string copy of the original string with its characters reversed in order.

Example

const str = "Hello, world!"
str = str.reverse()
print(str)  // "!dlrow ,olleH"

String.split

method string

Signature

function String:split(separator='', maxsplit=nil)

Description

The split method

Parameters

separator
A string that specified the character(s) to use as the separator. If separator is not specified, the string is split on any white space " ".
maxsplit
An optional number that specified the maximum number of splits allowed to perform. If maxsplit is not specified, the string is split into as many sub-strings as possible.

Returns

The method returns a list of string created by splitting the original string at each occurrence of the specified separator value, maxsplit times.

Example

var str = "Hello, world! How are you today?"
str = str.split()
print(my_list)  // ["Hello,", "world!", "How", "are", "you", "today?"]

String.contains

method string

Signature

function String:contains(substr)

Description

The contains method checks whether the specified string is found within the original string.

Parameters

substring
The string to search for within the original string.

Returns

The method returns true if the substring is found within the original string, or false otherwise.

Example

var str = "Hello, world!"
var result = str.contains("world")
print(result)  // true

result = str.contains("foo")
print(result)  // false

String.startswith

method string

Signature

function String:startswith(prefix)

Description

The startswith method checks whether the original string starts with a specified string sequence.

Parameters

prefix
The string to search for at the start of the original string.

Returns

The method returns true if the original string starts with prefix, and false otherwise.

Example

var str = "Hello, world!"
var res = str.startswith("Hello")
print(result)  // true

res = str.startswith("foo")
print(result)  // false

String.endswith

method string

Signature

function String:endswith(suffix)

Description

The endswith method checks whether the original string ends with a specified string sequence.

Parameters

suffix
The string to search for at the end of the original string.

Returns

The method returns true if the original string ends with suffix, and false otherwise.

Example

var str = "Hello, world!"
var res = str.endswith("world!")
print(res)  // true

res = str.endswith("foo")
print(res)  // false

String.leftstrip

method string

Signature

function String:leftstrip()

Description

The leftstrip strips the original string of any leading white spaces.

Returns

The method returns a copy of the original string with leading white space removed.

Example

var str = "   Hello, world!"
str = str.leftstrip()
print(str)  // "Hello, world!"

String.rightstrip

method string

Signature

function String:rightstrip()

Description

The rightstrip method strips the original string of any trailing white spaces.

Returns

The method returns a copy of the original string with all trailing white spaces removed.

Example

var str = "Hello, world!   "
str = str.rightstrip()
print(str)  // "Hello, world!"

String.strip

method string

Signature

function String:strip()

Description

The strip method strips the original string of any leading and trailing white spaces.

Returns

The method returns a copy of the original string with all its trailing and leading white spaces removed.

Example

var str = "   Hello, world!   "
str = my_string.strip()
print(str)  // "Hello, world!"

String.count

method string

Signature

function String:count(substr)

Description

The count method counts the number of occurrences of a specified string string within the original string.

Parameters

substr
The string to search for within the original string.

Returns

The method returns the number of occurrences of substr within the original string.

Example

var str = "Hello, world! Hello, world!"
var res = str.count("Hello")
print(res)  // 2
res = str.count("foo")
print(res)  // 0

String.find

method string

Signature

function String:find(substr)

Description

The find method finds the index of the first occurrence of a specified string within the original string.

Parameters

substr
The string to search for within the original string.

Returns

The method returns the index of the first occurrence of substr within the original string, or -1 if the string if not found.

Example

var str = "Hello, world!"
var res = str.find("world")
print(res)  // 7
res = str.find("foo")
print(res)  // -1

String.replace

method string

Signature

function String:replace(oldstring, newstring)

Description

The replace method replaces all occurrences found of a specified string from the original string with a different string sequence.

Parameters

oldstring
The old string occurrence present within the original string.
newstring
The string to replace oldstring with in the new string.

Returns

The method returns a copy of the original string with all occurrences of oldstring replaced with newstring. If no occurrences have been found, the returned string is exactly the same original string value.