Class Support.Str
The Str support class contains helper functions to manipulate strings.
Usage:
-- library is an instance of the Stormwind Library library.str
Methods
support.str:isEmpty (value) | Determines whether a string is empty or not. |
support.str:isNotEmpty (value) | Determines whether a string is not empty. |
support.str:isQuoted (value) | Determines whether a string is quoted by " or '. |
support.str:isWrappedBy (value, wrapper, endWrapper) | Determines whether a string is wrapped by a prefix and a suffix. |
support.str:removeQuotes (value) | Removes quotes from a string. |
support.str:removeWrappers (value, wrapper, endWrapper) | Removes the wrapping strings from a string. |
support.str:replaceAll (value, find, replace) | Replaces all occurrences of a substring in a string with another substring. |
support.str:split (value, separator) | Splits a string in a table by breaking it where the separator is found. |
support.str:trim (value) | Removes all whitespace from the beginning and end of a string. |
support.str:ucFirst (value) | Returns the given string with the first character capitalized. |
Methods
- support.str:isEmpty (value)
-
Determines whether a string is empty or not.
By empty, it means that the string is nil, has no characters, or has only whitespace characters. This last case is important because a string with only whitespace characters is not considered empty by Lua's standards, but it is by this function's standards.
If a method shouldn't consider a string with only whitespace characters as empty, please do not use this function.
Parameters:
- value string the string to be checked
Returns:
-
boolean
whether the string is empty or not
Usage:
local value = " " library.str:isEmpty(value) -- true
- support.str:isNotEmpty (value)
-
Determines whether a string is not empty.
This function is the opposite of Str:isEmpty.
Parameters:
- value string the string to be checked
Returns:
-
boolean
whether the string is not empty
Usage:
local value = " " library.str:isNotEmpty(value) -- false
- support.str:isQuoted (value)
-
Determines whether a string is quoted by " or '.
Parameters:
- value string the string to be checked
Returns:
-
boolean
whether the string is quoted or not
Usage:
local value = "'quoted'" library.str:isQuoted(value) -- true
- support.str:isWrappedBy (value, wrapper, endWrapper)
-
Determines whether a string is wrapped by a prefix and a suffix.
This function is useful to determine if a string is wrapped by a pair of strings, like quotes, parentheses, brackets, etc.
The third parameter is optional. If it is not provided, the function will assume that the prefix and suffix are the same.
Finally, this function will return true if the string contains only the prefix and suffix, like "", "()", "[]", etc. That would mean that an empty string is considered wrapped by something.
Parameters:
- value string the string to be checked
- wrapper string the prefix of the wrapping
- endWrapper optional string the suffix of the wrapping, will assume wrapper if not provided
Returns:
-
boolean
whether the string is wrapped by the prefix and suffix
Usage:
local value = "'quoted'" library.str:isWrappedBy(value, "'") -- true
- support.str:removeQuotes (value)
-
Removes quotes from a string.
This method can't simply call removeWrappers twice for " or ', because the string could be wrapped by one type of quote and contain the other type inside it, so it first checks which type of quote is wrapping the string and then removes it.
Parameters:
- value string the string to be checked
Returns:
-
string
the string without quotes
Usage:
local value = "'quoted'" library.str:removeQuotes(value) -- quoted
- support.str:removeWrappers (value, wrapper, endWrapper)
-
Removes the wrapping strings from a string.
This function is useful to remove quotes, parentheses, brackets, etc, from a string.
Similarly to Str:isWrappedBy, the third parameter is optional. If it is not provided, the function will assume that the prefix and suffix are the same.
Parameters:
- value string the string to be checked
- wrapper string the prefix of the wrapping
- endWrapper optional string the suffix of the wrapping, will assume wrapper if not provided
Returns:
-
string
the string without the prefix and suffix
Usage:
local value = "'quoted'" library.str:removeWrappers(value, "'") -- quoted
- support.str:replaceAll (value, find, replace)
-
Replaces all occurrences of a substring in a string with another
substring.
This function does not support regular expressions. If regular expressions are needed, please use Lua's string.gsub function. It was created for the convenience of allowing quick replacements that also accept characters like ".", "(", "[", etc, that would be interpreted as regular expressions metacharacters.
Parameters:
- value string the subject string to have the replacements
- find string the substring to be replaced
- replace string the substring to replace the find substring
Returns:
-
string
the string after the replacements
Usage:
local value = "Hello, world!" library.str:replaceAll(value, "world", "Lua") -- Hello, Lua!
- support.str:split (value, separator)
-
Splits a string in a table by breaking it where the separator is found.
Parameters:
Returns:
-
table
a table with the split strings
Usage:
local value = "Hello, world!" library.str:split(value, ", ") -- { "Hello", "world!" }
- support.str:trim (value)
-
Removes all whitespace from the beginning and end of a string.
Parameters:
- value string the string to be trimmed
Returns:
-
string
the string without whitespace at the beginning and end
Usage:
local value = " Hello, world! " library.str:trim(value) -- "Hello, world!"
- support.str:ucFirst (value)
-
Returns the given string with the first character capitalized.
Parameters:
- value string The string to have the first character capitalized
Returns:
-
string
The string with the first character capitalized
Usage:
local value = "hello, world!" library.str:ucFirst(value) -- "Hello, world!"