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 |
|---|---|
Left-justify a string | |
Right-justify a string | |
Given a string, return hexadecimal values | |
Given hexadecimal values, return a string | |
Check if a string starts with a given substring | |
Check if a string ends with a given substring | |
Remove characters from the left of a string | |
Remove characters from the right of a string | |
Split a string into a table of strings | |
Remove spaces on the left and right of a string |
Return the string left-justified in a string of length width.
Parameters:
input-string(string) — the string to left-justifywidth(integer) — the width of the string after left-justifyingpad-character(string) — a single character, default = 1 space
Returns
left-justified string (unchanged if width <= string length)
Return type
string
Example:
tarantool> string = require('string')---...tarantool> string.ljust(' A', 5)---- ' A '...
Return the string right-justified in a string of length width.
Parameters:
input-string(string) — the string to right-justifywidth(integer) — the width of the string after right-justifyingpad-character(string) — a single character, default = 1 space
Returns
right-justified string (unchanged if width <= string length)
Return type
string
Example:
tarantool> string = require('string')---...tarantool> string.rjust('', 5, 'X')---- 'XXXXX'...
Return the hexadecimal value of the input string.
Parameters:
input-string(string) — the string to process
Returns
hexadecimal, 2 hex-digit characters for each input character
Return type
string
Example:
tarantool> string = require('string')---...tarantool> string.hex('ABC ')---- '41424320'...
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
Returns
string with one byte for each pair of hexadecimal digits
Return type
string
Example:
tarantool> string = require('string')---...tarantool> string.fromhex('41424320')---- 'ABC '...
Return True if input-string starts with start-string, otherwise
return False.
Parameters:
input-string(string) — the string wherestart-stringshould be looked forstart-string(string) — the string to look forstart-pos(integer) — position: where to start looking withininput-stringend-pos(integer) — position: where to end looking withininput-string
Returns
true or false
Return type
boolean
start-pos and end-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...
Return True if input-string ends with end-string, otherwise return
False.
Parameters:
input-string(string) — the string whereend-stringshould be looked forend-string(string) — the string to look forstart-pos(integer) — position: where to start looking withininput-stringend-pos(integer) — position: where to end looking withininput-string
Returns
true or false
Return type
boolean
start-pos and end-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...
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, so string.lstrip(...,'ABC') does not mean strip 'ABC', it
means strip 'A' or 'B' or 'C'.
Parameters:
input-string(string) — the string to processlist-of-characters(string) — what characters can be stripped. Default = space.
Returns
result after stripping characters from input string
Return type
string
Example:
tarantool> string = require('string')---...tarantool> string.lstrip(' ABC ')---- 'ABC '...
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, so string.rstrip(...,'ABC') does not mean strip 'ABC', it
means strip 'A' or 'B' or 'C'.
Parameters:
input-string(string) — the string to processlist-of-characters(string) — what characters can be stripped. Default = space.
Returns
result after stripping characters from input string
Return type
string
Example:
tarantool> string = require('string')---...tarantool> string.rstrip(' ABC ')---- ' ABC'...
Split input-string into one or more output strings in a table. The
places to split are the places where split-string occurs.
Parameters:
input-string(string) — the string to splitsplit-string(string) — the string to find withininput-string. Default = spacemax(integer) — maximum number of delimiters to process counting from the beginning of the input string. Result will contain max + 1 parts maximum
Returns
table of strings that were split from input-string
Return type
table
Example:
tarantool> string = require('string')---...tarantool> string.split("A:B:C:D:F", ":", 2)---- - A- B- C:D:F...
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, so string.strip(...,'ABC') does not mean strip
'ABC', it means strip 'A' or 'B' or 'C'.
Parameters:
input-string(string) — the string to processlist-of-characters(string) — what characters can be stripped. Default = space.
Returns
result after stripping characters from input string
Return type
string
Example:
tarantool> string = require('string')---...tarantool> string.strip(' ABC ')---- ABC...