Module: gears.string
String module for gears.
Static module functions
gears.string.xml_escape (text) -> string | Escape a string from XML char. | |
gears.string.xml_unescape (text) -> string | Unescape a string from entities. | |
gears.string.linecount (text) -> int | Count number of lines in a string. | |
gears.string.linewrap (text, width, indent) -> string | Split a string into multiple lines. | |
gears.string.quote_pattern (s) -> string | Escape all special pattern-matching characters so that lua interprets them literally instead of as a character class. | |
gears.string.query_to_pattern (q) | Generate a pattern matching expression that ignores case. | |
gears.string.split (str, delimiter) -> table | Split separates a string containing a delimiter into the list of substrings between that delimiter. | |
gears.string.startswith (str, sub) -> boolean | Check if a string starts with another string. | |
gears.string.endswith (str, sub) -> boolean | Check if a string ends with another string. |
Static module functions
- gears.string.xml_escape (text) -> string
-
Escape a string from XML char.
Useful to set raw text in textbox.
Parameters:
- text Text to escape.
Returns:
-
string
Escaped text.
- gears.string.xml_unescape (text) -> string
-
Unescape a string from entities.
Parameters:
- text Text to unescape.
Returns:
-
string
Unescaped text.
- gears.string.linecount (text) -> int
-
Count number of lines in a string.
Usage example output:
Count is: 1 Count is: 3
Parameters:
- text string Input string.
Returns:
-
int
Number of lines.
Usage:
local test = "do.t" local res = gears.string.linecount(test) print("Count is: " .. res) local test2 = "do\nit\nnow" local res2 = gears.string.linecount(test2) print("Count is: " .. res2)
- gears.string.linewrap (text, width, indent) -> string
-
Split a string into multiple lines.
Usage example output:
do it
Parameters:
- text string String to wrap.
- width number Maximum length of each line. Default: 72.
- indent number Number of spaces added before each wrapped line. Default: 0.
Returns:
-
string
The string with lines wrapped to width.
Usage:
local test = "do it" local res = gears.string.linewrap(test, 2, 0) print(res)
- gears.string.quote_pattern (s) -> string
-
Escape all special pattern-matching characters so that lua interprets them literally instead of as a character class. Source: http://stackoverflow.com/a/20778724/15690
Usage example output:
do%.it
Parameters:
- s string String to generate pattern for
Returns:
-
string
string with escaped characters
Usage:
local test = "do.it" local res = gears.string.quote_pattern(test) print(res)
- gears.string.query_to_pattern (q)
-
Generate a pattern matching expression that ignores case.
Parameters:
- q string Original pattern matching expression.
- gears.string.split (str, delimiter) -> table
-
Split separates a string containing a delimiter into the list of
substrings between that delimiter.
Parameters:
Returns:
-
table
list of the substrings
- gears.string.startswith (str, sub) -> boolean
-
Check if a string starts with another string.
Usage example output:
true false false
Parameters:
Returns:
-
boolean
true
if string starts with specified stringUsage:
local test = "do.it" local res = gears.string.startswith(test,"do") print(tostring(res)) -- Print boolean value res = gears.string.startswith(test,"it") print(tostring(res)) -- print boolean value res = gears.string.startswith(nil,"do") print(tostring(res)) -- print boolean value
- gears.string.endswith (str, sub) -> boolean
-
Check if a string ends with another string.
Usage example output:
true false false
Parameters:
Returns:
-
boolean
true
if string ends with specified stringUsage:
local test = "do.it" local res = gears.string.endswith(test,"it") print(tostring(res)) res = gears.string.endswith(test,"do") print(tostring(res)) res = gears.string.endswith(nil,"it") print(tostring(res))