# 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:
* Navigate to Module:Sandbox.
* 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.
* Add the following code and save the page:

## Test Your Lua Script

To test your Lua script:
* Navigate to either the Module_talk:Sandbox page, the Wikiversity:Sandbox page, or your own user or sandbox page.
* 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:
* string.byte('A') returns the numeric ASCII value of the character(s) in the string.
* string.char(65) returns the character equivalent of the ASCII value(s) given.
* string.find('that', 'at') returns the numeric position of the second string within the first string.
* string.format('%.2f', 0) returns the second and any additional parameters formatted based on the first parameter string.
    See fprintf for format specifiers.
* 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.
* 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.
* string.len('len') returns the length of the string.
* string.lower('LOWER') returns the string converted to lower case.
* 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.
* string.rep('*', 5) returns the string repeated the given number of times.
* string.reverse('reverse') returns the string reversed.
* string.sub('that', 2, 3) returns the substring of the first parameter starting at the second parameter and ending at the third parameter.
* string.upper('upper') 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.

## See Also

* Lua Tables