string.find

From ComputerCraft
Jump to: navigation, search

string.find
Function
Syntax
string.find()
Colon notation: sentence:find()

Returns number
API string
Source Lua (source)

Finds a pattern in a sentence.

ExampleExampleFind the word hello
Returns the start and end pos of the word.
Code
string.find("hello world","hello")
Output 1 5
ExampleExampleFind the word hello with start at pos 2
Returns nil, because it starts at pos 2.
Code
string.find("hello world","hello",2)
Output nil
ExampleExampleFind the letter "l" in the last 6 letters
Returns the start and end pos of the letter "l" in the word world.
Code
string.find("hello world","l",-6)
Output 10 10
ExampleExampleFinding without pattern
Normaly it would find only the letter "l". When you turn the pattern finding off, it finds the letter "%" as well.
Code
string.find("%l","%l",1,true)
Output 1 2