To write a LUA implementation of PHP's Strpos () function, find a string at the first occurrence of the specified string position, in fact, Lua also provides us with such a function to use String.find () can be obtained, we still write a simple function, the following code:
Copy Code code as follows:
function Strpos (str, f)
If Str ~= nil and F ~= Nil Then
Return (String.find (str, f))
Else
return Nil
End
End
Test as shown in the following illustration:
Here's a strrpos () function that finds the last occurrence of a string in the specified string, and let's just write the function as follows:
Copy Code code as follows:
function Strrpos (str, f)
If Str ~= nil and F ~= Nil Then
Local T = True
Local offset = 1
Local result = Nil
while (t)
Todo
Local TMP = String.find (str, F, offset)
If TMP ~= Nil Then
offset = offset + 1
result = tmp
Else
T = False
End
End
return result
Else
return Nil
End
End
Test the following diagram (note: If you want to find. Escape is required, use "%.") :
Okay, let's get here today, and we'll continue to implement other function functions.