Suel Ahmed
Suel Ahmed

Reputation: 63

string.find function functionality in Lua

Below is my piece of code

str = "abc@hotmailcom"
a = string.find(str , "@")
print(a)

My output is 4 i.e place value of "@"

But when I run the following piece of code

str = "abc@hotmailcom"
a = string.find(str , ".")
print(a)

instead of outputting "nil", the program returns the value 1.

I am new to Lua language so could anyone explain to me what is happening and how string.find() works in lua?

Upvotes: 1

Views: 668

Answers (1)

csaar
csaar

Reputation: 560

. is a character class for all characters in lua pattern, you have to to escape it with %. See Lua Patterns

> print(string.find("abc@hotmailcom", "."))
1       1
> print(string.find("abc@hotmailcom", "%."))
nil

Upvotes: 1

Related Questions