string.sub

From ComputerCraft
Jump to: navigation, search

string.sub
Function
Syntax
string.sub()
Colon notation: str:sub()

Returns string
API string
Source Lua (source)

Return a string including the characters from start to end within str inclusively. If the arguments are negative, the character count will wrap the other way around. If the last argument end is not given, the substring will end at the end of the initial string.

ExampleExampleStrip the first and last characters from users input
Read a line from the user, then print the same string without the first and last characters
Code
print(string.sub(read(), 2, -2))
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the string printed would be ello Worl.
ExampleExampleReturn ending of a string
Read a line from the user, then return the last 7 characters if the string.
Code
print(string.sub(read(), -7))
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the string printed would be o World.