Lua Lua function of the character processing -TTLSA (d)

Defined function is based on the beginning of the function keyword, followed by the name of the function, then the parameters to be passed to the function, without parameters passed to the function, still need to use () to represent an empty list of parameters to the end of the end keyword .
function 函数名()
	...
	...
	...
end
1. The single parameter:
function F_1(var)
	print("My website is: "  var)
end
Var parameters passed to the function, and the function used in the while, the function parameters are local variables, recovered after the end of the function call. 2. Multiple parameters
function F_2(var1, var2)
	print("My website is: "  var1)
	print("QQ群: " var2)
end
A plurality of transmission parameters, separated by commas. 3. Lua variable parameters may also define variable length argument list, use the (...) instead of the parameter list. Lua will create a local name for the arg the table, to save all the parameters passed in a function call, as well as the number of parameters to get through arg.n. 4. The return value of the function using the return key and keep up with the variable name to return the results. Return the plurality of result, separated by commas. The character processing function is a character string Lua powerful processing capabilities, can be extended with a pattern matching function and many character processing function used. 5.1 string.len (string) function tells the number of characters.
> s = 'www.ttlsa.com'
> print(string.len(s))
13
5.2 string.sub (string, start, end) returns the substring of the specified string. start parameter specified start position, end the specified end position.
> s = 'www.ttlsa.com'
> ns = string.sub(s,3,6)
> print(ns)
w.tt
You can also specify the start parameter is negative, finally counted from the starting position of the string. end parameters can be omitted, returns the substring from start to end of the string. 5.3 string.format () specified string formatted output. 5.4 string.find (source_str, find_str) function queries the first character in a position consistent with find_str source_str in. If found its start and end positions is returned, did not find returns nil
> s="My website: www.ttlsa.com"
> print(string.find(s,"ttlsa"))
17 21
> print(string.find(s,"w")) 
4 4
> print(string.find(s,"xx"))
nil
5.5 string.gsub (source_str, pattern, replacement_str, [num]) string.gsub function returns a string, character string source_str matches of pattern will be replaced replacement_str.
> s="My website: www.ttlsa.com"
> print(string.gsub(s,'ttlsa','TTLSA'))
My website: www.TTLSA.com 1
An optional parameter may be added at the end of the function, it is used to specify the number of times to be replaced.
> print(string.gsub(s,'w','XXXXXX')) 
My XXXXXXebsite: XXXXXXXXXXXXXXXXXX.ttlsa.com 4
> print(string.gsub(s,'w','XXXXXX',2))
My XXXXXXebsite: XXXXXXww.ttlsa.com 2
5.6 string.gfind (source_str, pattern) traversing a string that matches the specified string once to return the substring.

Reproduced in: https: //my.oschina.net/766/blog/211473

Guess you like

Origin blog.csdn.net/weixin_33735676/article/details/91546282