string.sub

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.

ExampleStrip 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
<nowiki>
print(string.sub(read(), 2, -2))
    </nowiki>
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the string printed would be ello Worl.
ExampleReturn ending of a string
Read a line from the user, then return the last 7 characters if the string.
Code
<nowiki>
print(string.sub(read(), -7))
    </nowiki>
Output Depends on what the user wrote. For instance, if they were to enter Hello World, the string printed would be o World.

string.sub
Function
Syntax
string.sub(
  • str : string
  • start : number
  • end? : number
)
Colon notation: str:sub(
  • start : number
  • end? : number
)

Returns string
API string
Source Lua (source)
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.