string format(... arguments)¶
string rep(number n)¶
nThe count of copies.
string sub(number i, number j)¶
iThe start index.
jThe end index.
string , number string.gsub(string s, string pattern, string repl, number n)¶
replThe replacement pattern.
nThe maximum count of replacements.
1 2 3 4
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)¶
initThe start position to find the pattern.
plainTrue to use plain pattern string instead of pattern match.
1 2 3 4 5 6
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)¶
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.
initThe start index to match.
string string.gmatch(string s, string pattern)¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
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.