string.sub()

Type Function
Library string.*
Return value String
Revision Release 2024.3703
Keywords string, sub, substring
See also string.gsub()
String

Overview

Returns a substring (a specified portion of an existing string).

Syntax

string.sub( s, i [, j] )

s:sub( i [,j] )
s (required)

String. The string.

i (required)

Number. Index of the start of the substring (character position).

j (optional)

Number. Index of the end of the substring (inclusive). If not specified, the substring ends at the end of the string.

Example

local s = "Hello Corona user"
print( s:sub( 7 ) )        --> Corona user
print( s:sub( 7, 9 ) )     --> Cor
print( s:sub( -11 ) )      --> Corona user
print( s:sub( -11, 12 ) )  --> Corona
print( string.sub( "Hello Corona user", -11, -6 ) )  --> Corona