string.find

string.find
Function
Syntax
string.find(
  • sentence : string
  • pattern : string
  • start? : number
  • plain? : boolean
)
Colon notation: sentence:find(
  • pattern : string
  • start? : number
  • plain? : boolean
)

Returns number
API string
Source Lua (source)

Finds a pattern in a sentence.

ExampleFind the word hello
Returns the start and end pos of the word.
Code
<nowiki>
string.find("hello world","hello")
    </nowiki>
Output 1 5
ExampleFind the word hello with start at pos 2
Returns nil, because it starts at pos 2.
Code
<nowiki>
string.find("hello world","hello",2)
    </nowiki>
Output nil
ExampleFind the letter "l" in the last 6 letters
Returns the start and end pos of the letter "l" in the word world.
Code
<nowiki>
string.find("hello world","l",-6)
    </nowiki>
Output 10 10
ExampleFinding without pattern
Normaly it would find only the letter "l". When you turn the pattern finding off, it finds the letter "%" as well.
Code
<nowiki>
string.find("%l","%l",1,true)
    </nowiki>
Output 1 2
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.