Skip to content

string

The String library provides a set of functions for working with strings in Lua. These functions can be used to manipulate and transform strings, search for patterns in strings, and format strings for output.

Functions

string format(... arguments)

ReturnsThe result string.
Function is used to format strings based on a specified format string and any additional values that need to be included in the resulting string. The function takes a format string as its first argument, which defines the structure and contents of the resulting string, and one or more additional arguments that are used to populate the format string.
  • arguments The arguments.

string rep(number n)

ReturnsThe result.
Function returns a new string that consists of n copies of the input string s. The function takes two arguments: the input string s, and an integer n that specifies how many copies of s to include in the resulting string.
  • n The count of copies.

string reverse()

ReturnsThe new string.
Function that takes a string as an argument and returns a new string with the characters in reverse order.

string sub(number i, number j)

ReturnsThe new string.
Function that returns a substring of string s that starts at index i and ends at index j (or the end of the string if j is not specified). If i is negative, the starting index is considered to be the end of the string minus the absolute value of i plus 1. If j is negative, the ending index is considered to be the end of the string minus the absolute value of j. If j is not specified, the function returns the substring starting from index i to the end of the string. If both i and j are not specified, the function returns the entire string.
  • i The start index.
  • j The end index.

string lower()

ReturnsThe new string.
Function that used to convert a given string to lowercase. It takes a single parameter, a string, and returns a new string with all uppercase characters converted to lowercase.

string upper()

ReturnsThe new string.
Function returns a new string with all the characters in the given string converted to uppercase.

number len()

ReturnsThe length.
Function that used to get the length of a string. It takes a single parameter, a string, and returns the length of the string as an integer. The length of the string is the number of characters in the string.

string , number string.gsub(string s, string pattern, string repl, number n)

ReturnsThe result string and the total number of matches.
Function that used to replace all occurrences of a pattern in a string with a replacement string. It takes three required parameters: the original string s, the pattern to be searched for, and the replacement string repl. It also takes an optional fourth parameter n, which specifies the maximum number of substitutions to be made.
  • s A string.
  • pattern A pattern.
  • repl The replacement pattern.
  • n The maximum count of replacements.
1
2
3
4
local s = "The quick brown fox jumps over the lazy dog"
local new_s, count = string.gsub(s, "%a+", "dog")
print(new_s) -- Output: "dog dog dog dog dog dog dog dog dog"
print(count)  -- Output: 9

In this example, we use string.gsub() to replace all words in a string s with the word "dog". The "%a+" pattern matches one or more consecutive alphabetical characters in the string.

The string.gsub() function takes a string s, a pattern, and a replacement string repl as its parameters. It returns a new string that is the result of replacing all occurrences of the pattern in the string with the replacement string. Additionally, string.gsub() returns a count of the number of replacements made.

In the example, we use string.gsub() to replace all words in the string with the word "dog". The resulting string is assigned to new_s, and the number of replacements made is assigned to count. The print() function is then used to print the resulting string and the number of replacements to the console.

string.gsub() is a useful tool for processing strings in Lua. By specifying a pattern and a replacement string, you can easily replace specific substrings in a string, or replace all occurrences of a particular pattern in a string.

number , number string.find(string s, string pattern, number init, boolean plain)

ReturnsThe start index and the end index if the pattern is matched.
Function that searches a given string s for a specified pattern pattern and returns the starting and ending indices of the first occurrence of the pattern in the string, or nil if the pattern is not found.
  • s A string.
  • pattern A pattern.
  • init The start position to find the pattern.
  • plain True to use plain pattern string instead of pattern match.
1
2
3
4
5
6
-- Define a string
local myString = "The quick brown fox jumps over the lazy dog"
-- Find the position of the word "fox" in the string
local startIndex, endIndex = string.find(myString, "fox")
-- Print the result
print(startIndex, endIndex) -- Output: 17 19

In this example, we define a string called myString, which is a classic pangram. We then use the string.find() function to search for the position of the word "fox" in the string.

The string.find() function takes two mandatory parameters: the string to search (myString) and the pattern to search for ("fox"). It also has two optional parameters: the starting index of the search (init) and whether or not to interpret the pattern as a plain string (plain).

In this example, we don't provide the optional init or plain parameters, so the default values are used. The string.find() function returns two values: the starting index of the match (in this case, 17) and the ending index of the match (in this case, 19).

We then print the starting and ending indices to the console. In this case, the output should be 17 and 19, which correspond to the positions of the letters "f" and "o" in the string.

string string.match(string s, string pattern, number init)

ReturnsThe matched substring, or all captures if no capture

Function used to search a string for a specified pattern and return the first match. It takes two required parameters: the string to search and the pattern to match, and an optional third parameter to specify the starting position in the string.

The pattern parameter can be a simple string, or a more complex pattern that uses Lua's pattern matching syntax. The pattern can include special characters that have a special meaning in pattern matching, such as ^ to match the beginning of a string, $ to match the end of a string, and . to match any single character.

The function returns the first substring that matches the pattern, or nil if no match is found. If the pattern includes captures, then the function returns the captured substrings as additional return values.

  • s A string.
  • pattern The pattern.
  • init The start index to match.

string string.gmatch(string s, string pattern)

ReturnsA function which can be used in for loop.
Function that is used to iterate over all occurrences of a pattern in a string. It takes two arguments: a string s and a pattern, and returns an iterator function that can be used in a loop to iterate over all matches of the pattern in the string.
  • s A string.
  • pattern The pattern.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
local s = "The quick brown fox jumps over the lazy dog"
-- Use a for loop to iterate over each word in the string
for word in string.gmatch(s, "%a+") do
    print(word)
end
-- output:
The
quick
brown
fox
jumps
over
the
lazy
dog

In this example, we use string.gmatch() to iterate over all the words in a string s. The "%a+" pattern matches one or more consecutive alphabetical characters in the string.

The string.gmatch() function takes a string s and a pattern as its parameters. It returns an iterator function that can be used in a loop to iterate over all occurrences of the pattern in the string.

In the example, we use a for loop to iterate over all the words in the string. For each match of the "%a+" pattern, the iterator function returns the matched substring, which is assigned to the word variable. The print() function is then used to print each word to the console.

string.gmatch() is a useful tool for processing strings in Lua. By specifying a pattern, you can easily extract specific substrings from a string, or iterate over all occurrences of a particular pattern in a string.