Lua/String Library

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. This lesson will show you how to use the Lua String library in your scripts.

Prerequisites
This lesson assumes you have already completed the Tables lesson.

Create a Lua Script that Uses the String Library
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.
 * 1) Add the following code and save the page:

Test Your Lua Script
To test your Lua script:
 * 1) Navigate to either the Module_talk:Sandbox page, the Sandbox page, or your own user or sandbox page.
 * 2) Add the following code and save the page:

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
To understand your Lua script:
 * 1)   returns the numeric ASCII value of the character(s) in the string.
 * 2)   returns the character equivalent of the ASCII value(s) given.
 * 3)   returns the numeric position of the second string within the first string.
 * 4)   returns the second and any additional parameters formatted based on the first parameter string.
 * See fprintf for format specifiers.
 * 1)   processes the first parameter by repeatedly returning a string matching the pattern specified by the second parameter.
 * See Patterns for pattern specifiers.
 * 1)   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.
 * 1)   returns the length of the string.
 * 2)   returns the string converted to lower case.
 * 3)   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.
 * 1)   returns the string repeated the given number of times.
 * 2)   returns the string reversed.
 * 3)   returns the substring of the first parameter starting at the second parameter and ending at the third parameter.
 * 4)   returns the string converted to upper case.

Conclusion
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.