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.


This lesson assumes you have already completed the Tables lesson.

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

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'
function p.char()
    return ';char\n:string.char(65) is ' .. string.char(65) .. '\n'
function p.find()
    return ';find\n:string.find(\'that\', \'at\') is ' .. string.find('that', 'at') .. '\n'
function p.format()
    return ';format\n:string.format(\'%.2f\', 0) is ' .. string.format('%.2f', 0) .. '\n'
function p.gmatch()
	local result = ';gmatch\n:'
	for word in string.gmatch('This is a test', '%w+') do
		result = result .. word .. '-'
    return result .. '\n'
function p.gsub()
    return ';gsub\n:string.gsub(\'This is a test\', \'%s\', \'-\') is ' .. string.gsub('This is a test', '%s', '-') .. '\n'
function p.len()
    return ';len\n:string.len(\'len\') is ' .. string.len('len') .. '\n'
function p.lower()
    return ';lower\n:string.lower(\'LOWER\') is ' .. string.lower('LOWER') .. '\n'
function p.match()
    return ';match\n:string.match(\'This is a test!\', \'.\', -1) is ' .. string.match('This is a test!', '.', -1) .. '\n'
function p.rep()
    return ';rep\n:string.rep(\'*\', 5) is ' .. string.rep('*', 5) .. '\n'
function p.reverse()
    return ';reverse\n:string.reverse(\'reverse\') is ' .. string.reverse('reverse') .. '\n'
function p.sub()
    return ';sub\n:string.sub(\'that\', 2, 3) is ' .. string.sub('that', 2, 3) .. '\n'
function p.upper()
    return ';upper\n:string.upper(\'upper\') is ' .. string.upper('upper') .. '\n'
return p

Test Your Lua Script[edit]

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:

The result should be:

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

Understand Your Lua Script[edit]

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.


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]