string.find()

Type Function
Library string.*
Return value Numbers
Revision Current Public Release (2017.3135)
Keywords string, find, search, patterns
See also string.gmatch()
string.gsub()
string.match()

Overview

Looks for the first match of a pattern in a string. If found, it returns the indices where the occurrence starts and ends; otherwise, returns nil.

Syntax

string.find( s, pattern [, init [, plain]] )
s (required)

String. The string.

pattern (required)

String. Specifies the pattern to match. See Lua String Manipulation.

init (optional)

Number. Specifies where to start the search. Default value is 1 and can be negative. Negative numbers start at the end of the string.

plain (optional)

Boolean. A value of true turns off the pattern matching facilities, so the function does a plain "find substring" operation with no characters in pattern being considered "magic". If plain is given, then the init argument must be provided as well.

Example

print( string.find( "Hello Corona user", "Corona" ) )        --> 7   12
print( string.find( "Hello Corona user", "Bud" ) )           --> nil
print( string.find( "Hello Corona user", "Corona", 1 ) )     --> start at first character: 7   12
print( string.find( "Hello Corona user", "Corona", 8 ) )     --> start at character 8: nil
print( string.find( "Hello Corona user", "e", -5 ) )         --> first "e" 5 characters from end: 16   16
print( string.find( "Hello Corona user", "%su", 1, true ) )  --> nil