Lua/String Library

From Wikiversity
< Lua
Jump to navigation Jump to search

Lua modules based on the Scribunto/Lua extension are stored in resource pages using the Module: namespace. Each module uses a table to hold functions and variables, and that containing table is returned at the end of the module code.[1] This lesson will show you how to use the Lua String library in your scripts.

Prerequisites[edit | edit source]

This lesson assumes you have already completed the Tables lesson.

Create a Lua Script that Uses the String Library[edit | edit source]

To create a Lua script that uses the String library:

  1. Navigate to Module:Sandbox.
  2. Clear all existing code.
    It's a sandbox. Everyone is free to play in the sandbox. But if you find another user is actively editing the sandbox at the same time, you may also use Module:Sandbox/Username, where Username is your Wikiversity username.
  3. Add the following code and save the page:
local p = {}
 
function p.byte()
    return ';byte\n:string.byte(\'A\') is ' .. string.byte('A') .. '\n'
end
 
function p.char()
    return ';char\n:string.char(65) is ' .. string.char(65) .. '\n'
end
 
function p.find()
    return ';find\n:string.find(\'that\', \'at\') is ' .. string.find('that', 'at') .. '\n'
end
 
function p.format()
    return ';format\n:string.format(\'%.2f\', 0) is ' .. string.format('%.2f', 0) .. '\n'
end
 
function p.gmatch()
	local result = ';gmatch\n:'
	for word in string.gmatch('This is a test', '%w+') do
		result = result .. word .. '-'
	end
    return result .. '\n'
end
 
function p.gsub()
    return ';gsub\n:string.gsub(\'This is a test\', \'%s\', \'-\') is ' .. string.gsub('This is a test', '%s', '-') .. '\n'
end
 
function p.len()
    return ';len\n:string.len(\'len\') is ' .. string.len('len') .. '\n'
end
 
function p.lower()
    return ';lower\n:string.lower(\'LOWER\') is ' .. string.lower('LOWER') .. '\n'
end
 
function p.match()
    return ';match\n:string.match(\'This is a test!\', \'.\', -1) is ' .. string.match('This is a test!', '.', -1) .. '\n'
end
 
function p.rep()
    return ';rep\n:string.rep(\'*\', 5) is ' .. string.rep('*', 5) .. '\n'
end
 
function p.reverse()
    return ';reverse\n:string.reverse(\'reverse\') is ' .. string.reverse('reverse') .. '\n'
end
 
function p.sub()
    return ';sub\n:string.sub(\'that\', 2, 3) is ' .. string.sub('that', 2, 3) .. '\n'
end
 
function p.upper()
    return ';upper\n:string.upper(\'upper\') is ' .. string.upper('upper') .. '\n'
end
 
return p

Test Your Lua Script[edit | edit source]

To test your Lua script:

  1. Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
  2. Add the following code and save the page:
{{#invoke:Sandbox|byte}}
{{#invoke:Sandbox|char}}
{{#invoke:Sandbox|find}}
{{#invoke:Sandbox|format}}
{{#invoke:Sandbox|gmatch}}
{{#invoke:Sandbox|gsub}}
{{#invoke:Sandbox|len}}
{{#invoke:Sandbox|lower}}
{{#invoke:Sandbox|match}}
{{#invoke:Sandbox|rep}}
{{#invoke:Sandbox|reverse}}
{{#invoke:Sandbox|sub}}
{{#invoke:Sandbox|upper}}

The result should be:

byte
string.byte('A') is 65
char
string.char(65) is A
find
string.find('that', 'at') is 3
format
string.format('%.2f', 0) is 0.00
gmatch
This-is-a-test-
gsub
string.gsub('This is a test', '%s', '-') is This-is-a-test
len
string.len('len') is 3
lower
string.lower('LOWER') is lower
match
string.match('This is a test!', '.', -1) is !
rep
string.rep('*', 5) is *****
reverse
string.reverse('reverse') is esrever
sub
string.sub('that', 2, 3) is ha
upper
string.upper('upper') is UPPER

Understand Your Lua Script[edit | edit source]

To understand your Lua script:

  1. string.byte('A') returns the numeric ASCII value of the character(s) in the string.
  2. string.char(65) returns the character equivalent of the ASCII value(s) given.
  3. string.find('that', 'at') returns the numeric position of the second string within the first string.
  4. string.format('%.2f', 0) returns the second and any additional parameters formatted based on the first parameter string.
    See fprintf for format specifiers.
  5. for word in string.gmatch('This is a test', '%w+') do processes the first parameter by repeatedly returning a string matching the pattern specified by the second parameter.
    See Patterns for pattern specifiers.
  6. string.gsub('This is a test', '%s', '-') returns the first parameter with each occurrence of the pattern indicated by the second parameter replaced by the third parameter.
    See Patterns for pattern specifiers.
  7. string.len('len') returns the length of the string.
  8. string.lower('LOWER') returns the string converted to lower case.
  9. string.match('This is a test!', '.', -1) returns the substring of the first parameter matching the second parameter beginning at the position specified by the third parameter.
    See Patterns for pattern specifiers.
  10. string.rep('*', 5) returns the string repeated the given number of times.
  11. string.reverse('reverse') returns the string reversed.
  12. string.sub('that', 2, 3) returns the substring of the first parameter starting at the second parameter and ending at the third parameter.
  13. string.upper('upper') returns the string converted to upper case.

Conclusion[edit | edit source]

Congratulations! You've now created, tested, and understood a Lua script that uses the String library. Return to the main Lua page to learn about other Lua code libraries.

See Also[edit | edit source]

References[edit | edit source]