Module str
String utilities module
Functions
ensureLastChar (str, char) | Ensure the last character of a string If the last character of the given string is correct do nothing, otherwise add the character at the end of the string |
contains (str, pattern) | Check if a pattern is contained in a string |
startWith (str, pattern) | Check if a string starts with a specified string |
toArray (str) | Convert a string to an array with an element for each character of the string |
count (str, str2) | Count the number of occurrences of a character in a string |
Functions
- ensureLastChar (str, char)
-
Ensure the last character of a string
If the last character of the given string is correct do nothing, otherwise add the character at the end of the stringParameters:
- str string The string to ensure the last character of
- char string The string to ensure the last character of
Returns:
-
string # A string containing the last character
Raises:
error if str is not a string
error if char is not a charUsage:
print(ensureLastChar("Hello", "!") -- Hello!
- contains (str, pattern)
-
Check if a pattern is contained in a string
Parameters:
- str string The string to search the pattern in
- pattern string The pattern to search in the string
Returns:
-
boolean # true if str contains at least one time pattern, false otherwise
Raises:
error if str is not a string
error if pattern is not a stringUsage:
local str = "This is me Mario!" if contains(str, "Mario") then print("The string contains 'Mario' at least one time") else print("The string does not contains 'Mario'") end
- startWith (str, pattern)
-
Check if a string starts with a specified string
Parameters:
- str string The string to search the pattern in
- pattern string The pattern to search at the start of the string
Returns:
-
boolean # true if str starts with pattern, false otherwise
Raises:
error if str is not a string
error if pattern is not a stringUsage:
local str = "This is me Mario!" if startWith(str, "This") then print("The string starts with 'This'") else print("The string does not starts with 'This'") end
- toArray (str)
-
Convert a string to an array with an element for each character of the string
Parameters:
- str string The string to convert into an array
Returns:
-
table # The string as an array of letters
Raises:
error if str is not a stringUsage:
local str = "Hello" local strAsArr = toArray(str) -- { "H", "e", "l", "l", "o" }
- count (str, str2)
-
Count the number of occurrences of a character in a string
Parameters:
- str string The string to search in
- str2 string The string to search in str
Returns:
-
number # The number of times str2 occurs in str
Raises:
error if str is not a string
error if str2 is not a string
error if str2 is emptyUsage:
local str = "Hello" local nbr = count(str, "l") -- 2