Module string
The string
module has everything in the
standard Lua string library, and some
Tarantool extensions.
In this section we only discuss the additional functions that the Tarantool developers have added.
Below is a list of all additional string
functions.
Name | Use |
---|---|
string.ljust() | Left-justify a string |
string.rjust() | Right-justify a string |
string.hex() | Given a string, return hexadecimal values |
string.fromhex() | Given hexadecimal values, return a string |
string.startswith() | Check if a string starts with a given substring |
string.endswith() | Check if a string ends with a given substring |
string.lstrip() | Remove characters from the left of a string |
string.rstrip() | Remove characters from the right of a string |
string.split() | Split a string into a table of strings |
string.strip() | Remove spaces on the left and right of a string |
-
string.
ljust
(input-string, width[, pad-character])¶ Return the string left-justified in a string of length
width
.Parameters: Return: left-justified string (unchanged if width <= string length)
Rtype: string
Example:
tarantool> string = require('string') --- ... tarantool> string.ljust(' A', 5) --- - ' A ' ...
-
string.
rjust
(input-string, width[, pad-character])¶ Return the string right-justified in a string of length
width
.Parameters: Return: right-justified string (unchanged if width <= string length)
Rtype: string
Example:
tarantool> string = require('string') --- ... tarantool> string.rjust('', 5, 'X') --- - 'XXXXX' ...
-
string.
hex
(input-string)¶ Return the hexadecimal value of the input string.
Parameters: - input-string (
string
) – the string to process
Return: hexadecimal, 2 hex-digit characters for each input character
Rtype: string
Example:
tarantool> string = require('string') --- ... tarantool> string.hex('ABC ') --- - '41424320' ...
- input-string (
-
string.
fromhex
(hexadecimal-input-string)¶ Given a string containing pairs of hexadecimal digits, return a string with one byte for each pair. This is the reverse of
string.hex()
. The hexadecimal-input-string must contain an even number of hexadecimal digits.Parameters: - hexadecimal-input-string (
string
) – string with pairs of hexadecimal digits
Return: string with one byte for each pair of hexadecimal digits
Rtype: string
Example:
tarantool> string = require('string') --- ... tarantool> string.fromhex('41424320') --- - 'ABC ' ...
- hexadecimal-input-string (
-
string.
startswith
(input-string, start-string[, start-pos[, end-pos]])¶ Return True if
input-string
starts withstart-string
, otherwise return False.Parameters: Return: true or false
Rtype: boolean
start-pos
andend-pos
may be negative, meaning the position should be calculated from the end of the string.Example:
tarantool> string = require('string') --- ... tarantool> string.startswith(' A', 'A', 2, 5) --- - true ...
-
string.
endswith
(input-string, end-string[, start-pos[, end-pos]])¶ Return True if
input-string
ends withend-string
, otherwise return False.Parameters: Return: true or false
Rtype: boolean
start-pos
andend-pos
may be negative, meaning the position should be calculated from the end of the string.Example:
tarantool> string = require('string') --- ... tarantool> string.endswith('Baa', 'aa') --- - true ...
-
string.
lstrip
(input-string[, list-of-characters])¶ Return the value of the input string, after removing characters on the left. The optional
list-of-characters
parameter is a set not a sequence, sostring.lstrip(...,'ABC')
does not mean strip'ABC'
, it means strip'A'
or'B'
or'C'
.Parameters: Return: result after stripping characters from input string
Rtype: string
Example:
tarantool> string = require('string') --- ... tarantool> string.lstrip(' ABC ') --- - 'ABC ' ...
-
string.
rstrip
(input-string[, list-of-characters])¶ Return the value of the input string, after removing characters on the right. The optional
list-of-characters
parameter is a set not a sequence, sostring.rstrip(...,'ABC')
does not mean strip'ABC'
, it means strip'A'
or'B'
or'C'
.Parameters: Return: result after stripping characters from input string
Rtype: string
Example:
tarantool> string = require('string') --- ... tarantool> string.rstrip(' ABC ') --- - ' ABC' ...
-
string.
split
(input-string[, split-string[, max]])¶ Split
input-string
into one or more output strings in a table. The places to split are the places wheresplit-string
occurs.Parameters: Return: table of strings that were split from
input-string
Rtype: table
Example:
tarantool> string = require('string') --- ... tarantool> string.split("A:B:C:D:F", ":", 2) --- - - A - B - C:D:F ...
-
string.
strip
(input-string[, list-of-characters])¶ Return the value of the input string, after removing characters on the left and the right. The optional
list-of-characters
parameter is a set not a sequence, sostring.strip(...,'ABC')
does not mean strip'ABC'
, it means strip'A'
or'B'
or'C'
.Parameters: Return: result after stripping characters from input string
Rtype: string
Example:
tarantool> string = require('string') --- ... tarantool> string.strip(' ABC ') --- - ABC ...