string
:split(separator: string?, limit: number?): array
:slice(i: number?, j: number?): string
:replace(search_value: string, replace_value: string): string
:tolowercase(): string
:touppercase(): string
:reverse(): string
:contains(substr: string): boolean
:trim(): string
:trimstart(): string
:trimend(): string
string:split(separator: string?, limit: number?): array
Splits a string into substrings using the specified separator and return them as an array.
str
- A string to be splitted.separator
- A string that identifies the character or characters to use in separating the string. If omitted, a single-element table containing the entire string is returned.limit
- A value used to limit the number of elements returned in the table.
require 'lext'
local text = 'Hello world!'
text = text:split' ' -- { 'Hello', 'world!' }
print(text.join', ') -- Hello, world!
require 'lext'
local text = 'Hello world! Nothing'
text = text:split(' ', 2) -- { 'Hello', 'world!' }
print(text.join', ') -- Hello, world!
require 'lext'
local text = 'Thecut-hereperfectcut-heremoonlight!'
text = text:split('cut-here') -- { 'The', 'perfect', 'moonlight!' }
print(text.join' ') -- The perfect moonlight!
require 'lext'
local text = '?*%+*%The%perfect*%[(MOONLIGHT)].*%+^'
text = text:split('*%') --[[
{ '?', '+', 'The%perfect', '[(MOONLIGHT)].', '+^' }
]]
print(text.join', ') --[[
?, +, The%perfect, [(MOONLIGHT)]., +^
]]
require 'lext'
local text = 'The20perfecttrue20moonlight!'
text = text:split(20) -- { 'The', 'perfecttrue', 'moonlight!' }
print(text.join', ') -- The, perfecttrue, moonlight!
text = table.concat(text, ' ')
text = text:split(true) -- { 'The', 'perfect', 'moonlight!' }
print(text.join'') -- The perfect moonlight!
string:slice(i: number?, j: number?): string
Returns a section of a string.
i
- The index to the beginning of the specified portion of string.j
- The index to the end of the specified portion of string. If not specified, the substring continues until the end of the string.
require 'lext'
local emoji = '😎🤩💀😺'
-- :sub() - Returns some unknown characters.
print(emoji:sub(2)) -- ���🤩💀😺
print(emoji:sub(2, 2)) -- �
-- :slice() - Cuts you a break returning what you expect.
print(emoji:slice(2)) -- 🤩💀😺
print(emoji:slice(2, 2)) -- 🤩
string:replace(search_value: string, replace_value: string): string
Replaces a substring in the string using a search string.
search_value
- A search string.replace_value
- A string containing the substring to replace for each successful match ofsearch_value
.
require 'lext'
local text = '[Age: 50?]true'
text = text
:replace(50, 31) -- [Age: 31?]true
:replace(true, '') -- [Age: 31?]
:replace('?', '') -- [Age: 31]
:replace('[', '') -- Age: 31]
:replace(']', '') -- Age: 31
print(text)
string:tolowercase(): string
Returns a copy of string with all letters in lowercase.
require 'lext'
local apples = 'APPLE MAÇÃ MANZANA EPEL ΜΉΛΟ ЯБЛОКО'
print(apples:lower())
-- apple maÇÃ manzana epel ΜΉΛΟ ЯБЛОКО
print(apples:tolowercase())
-- apple maçã manzana epel μήλο яблоко
string:touppercase(): string
Returns a copy of string with all letters in uppercase.
require 'lext'
local apples = 'apple maçã manzana epel μήλο яблоко'
print(apples:upper())
-- APPLE MAçã MANZANA EPEL μήλο яблоко
print(apples:touppercase())
-- APPLE MAÇÃ MANZANA EPEL ΜΉΛΟ ЯБЛОКО
string:reverse(): string
A powerful update to support UTF8 characters.
local text = '.ãhnam alep oãhc oa erovrá ad aíac ãçam A'
print(text:reverse()) --[[
A ma�ã� ca��a da ��rvore ao ch��o pela manh��.
]]
require 'lext'
text = '.ãhnam alep oãhc oa erovrá ad aíac ãçam A'
print(text:reverse()) --[[
A maçã caía da árvore ao chão pela manhã.
]]
string:contains(substr: string): boolean
Checks if the given string contains a certain substring. If substr
string exists within the main string returns true
, otherwise returns false
.
substr
- A substring to search for.
Original issue by @Panquesito7
require 'lext'
local emoji = '😁😑😑😑👏😑😁'
print(emoji:contains'👏') -- true
print(emoji:contains'😅') -- false
string:trim(): string
Removes the leading and trailing white space line terminator characters from a string.
require 'lext'
local text = ' word '
print('<' .. text:trim() .. '>') -- <word>
string:trimstart(): string
Removes the leading white space characters from a string.
require 'lext'
local text = ' word '
print('<' .. text:trimstart() .. '>') -- <word >
string:trimend(): string
Removes the trailing white space line terminator characters from a string.
require 'lext'
local text = ' word '
print('<' .. text:trimend() .. '>') -- < word>
array(list: table)
.join(separator: string?): string
.slice(i: number?, j: number?): table
.filter(callback_fn(e: string?, i: number?, a: table?): any): table
.map(callback_fn(e: string?, i: number?, a: table?): any): table
.reduce(operator: string): number
.reverse(): table
.flat(depth: number?): table
require 'lext'
local a = array{1, 2, 3} .. array{'a', 'b', 'c'}
print(a.join', ') -- 1, 2, 3, a, b, c
array(list: table).join(separator: string?): string
Joins the items of the previous array into a string.
separator
- A string used to separate one array element from the next in the resulting string. If omitted, the array elements are separated with a comma.
require 'lext'
local a = array{'text', 4, 5.6, false, true}.join'/'
print(a) -- text/4/5.6/false/true
require 'lext'
local a = array{'text', 4, 5.6, false, true}.join()
print(a) -- text,4,5.6,false,true
array(list: table).slice(i: number?, j: number?): table
Returns a section of the array.
i
- The index to the beginning of the specified portion of the array.j
- The index to the end of the specified portion of the array. If not specified, the element continues until the end of the array.
require 'lext'
local a = array{'text', 4, 5.6, false, true}
.slice(2, 3) -- { 4, 5.6 }
print(a.join', ') -- 4, 5.6
require 'lext'
local a = array{'text', 4, 5.6, false, true}
.slice(2) -- { 4, 5.6, false, true }
.slice(2) -- { 5.6, false, true }
.slice(2) -- { false, true }
.slice(2) -- { true }
print(a.join', ') -- true
array(list: table)
.map(callback_fn(e: string?, i: number?, a: table?): any): table
Calls the callback function on each array item, returning a array with the results.
e
- Matches the item in array.i
- Matches the item index number.a
- Matches the array itself.
require 'lext'
local a = array{1, 2, 3, 4, 5}
.map(function (e)
return e + 10
end)
print(a.join', ') -- 11, 12, 13, 14, 15
require 'lext'
local a = array{
{
player = {
id = 1,
status = 'off'
}
},
{
player = {
id = 2,
status = 'on'
}
}
}.map(function (e)
e.player.status = 'on'
local res = array{
'Player ',
e.player.id,
' is ',
e.player.status,
'line'
}.join''
return res
end)
print(a.join'\n') --[[
Player 1 is online
Player 2 is online
]]
array(list: table)
.filter(callback_fn(e: string?, i: number?, a: table?): any): table
Calls the callback function on each array item, returning a array with the items that meet the specified condition.
e
- Matches the item in array.i
- Matches the item index number.a
- Matches the array itself.
require 'lext'
local a = array{18, 16, 15, 14, 21, 13, 23}
.filter(function (e)
return e >= 16
end)
print(a.join', ') -- 18, 16, 21, 23
require 'lext'
local a = array{
{
name = 'charmander',
e_type = {'fire'}
},
{
name = 'bulbasaur',
e_type = {'grass', 'poison'}
},
{
name = 'squirtle',
e_type = {'water'}
}
}.map(function (e)
local e_type = array(e.e_type)
.filter(function (f) return f == 'fire' end)
return #e_type > 0 and e.name or nil
end)
print(a.join', ') -- charmander
array(list: table)
.reduce(operator: '+' | '-' | '*' | '/' | '^' | '%' | '//'): number
Returns the accumulated result of all the elements in the array.
operator
- The operation symbol to be executed:+
- addition-
- subtraction*
- multiplication/
- division//
- rounded division%
- modulo^
- exponentiation
require 'lext'
local a = array{1, 2, 3, 4, 5}.reduce'*'
print(a) -- 120
require 'lext'
local grades = array{7.7, 8, 8.5, 7.1, 9, 9.2}
local avg = grades.reduce'+' / #grades
print(avg) -- 8.25
require 'lext'
local b_arr = array{true, false, true} -- { 1, 0, 1 }
.reduce'+'
print(b_arr) -- 2
array(list: table).reverse(): table
Reverses the elements order of the array.
require 'lext'
local pos = array{
'first', 'second', 'third',
'fourth', 'fifth', 'sixth'
}
print(pos.join' > ') --[[
first > second > third > fourth > fifth > sixth
]]
pos.reverse()
print(pos.join' < ') --[[
sixth < fifth < fourth < third < second < first
]]
array(list: table).flat(depth: number?): table: table
Returns a new array with all sub-array elements concatenated into it recursively up to the specified depth.
require 'lext'
local surface = array{
1, 2, 3, { 4, 5, 6 }, 7, 8, 9
}
print(surface.join', ') --[[
1, 2, 3, table: 0000026b6b4a0a20, 7, 8, 9
]]
surface = surface.flat()
print(surface.join', ') --[[
1, 2, 3, 4, 5, 6, 7, 8, 9
]]
require 'lext'
local deep = array{
1, 2, 3, {
'a', 'b', 'c', {
'x', 'y', 'z', {
4, true, false
}
}
}, 0
}
print(deep.join', ') --[[
1, 2, 3, table: 0000013ec51ad910, 0
]]
print(deep.flat().join', ') --[[
1, 2, 3, a, b, c, table: 0000013ec51ad950, 0
]]
print(deep.flat(2).join', ') --[[
1, 2, 3, a, b, c, x, y, z, table: 0000013ec51ad9d0, 0
]]
print(deep.flat(3).join', ') --[[
1, 2, 3, a, b, c, x, y, z, 4, true, false, 0
]]
Made with ❤️
Lunatic Fox - Josélio Júnior - 2023