lua string processing

string.byte(s [, i [, j]])

string.byte is used to convert ascii characters into numbers, s is the target string, i is an index start position (starting from 1), j is the index of the end position

string.char(...)

example

- default is a return of a value of ascii 
local R & lt = string.byte ( ' ABCDEFG ' )     - 97

- from the index 2 (b) to index 4 (d), respectively, i.e. ascii Returns the value bcd 
local R1, R2, R3 = string.byte ( ' ABCDEFG ' , 2 , . 4 )     - ... 98,99,100.

- Returns the 98 characters corresponding 
local R & lt = string.char ( 98 )     - A

- Back 98, 99, 100 together and the corresponding character returns 
local R & lt = string.char ( 98 , 99 , 100 )     - ABC

string.sub (s, i [, j])

Taken String (String split, string interception), i is the starting index, the ending index j is an optional parameter (included), that can be a negative number, as an index of the first character, a last character -1

example

local res,s
s = 'www.freecls.com'
res = string.sub(s,5)     --freecls.com
res = string.sub(s,5,-1)  --freecls.com

- after intercepting three 
RES = string.sub (S, - . 3 )     - COM

- taken before 3 
RES = string.sub (S, . 1 , . 3 )    - WWW

string.dump(function)

The sequence of functions into a string to hold it the next time you want to use directly loadstring or loadfile you can restore function

example

function say()
    print('hello')
end

local f_str = string.dump(say)
print(f_str)    --uaQ

- restoring function 
local FUNC = loadString (f_str)
func()

- If we f_str to save the file tmp.txt you can use loadfile ( 'tmp.txt') to restore function

string.find (s, pattern [, init [, plain]])

String search function can not find returns nil, found a return to the start position and end position, init is where to start the default is 1, plain default is false to use pattern matching, if true, said plain-text match (that is closed regular match)

example

local STR = ' I Love Programming, 11,22,% D + AA ' 
local S = to string.find has (STR, ' 222 ' )     - nil 
S = to string.find has (STR, ' Pro ' )   - . 8 
S = to string.find has (STR, " ,% D + " )     - . 19 (to match,. 11) 
S = to string.find has (STR, " ,% D + " , . 1 , to true )     - 25 (due to the closure of the matching pattern, so to match,% d +)

string.match (s, pattern [, init])

It string.find with similar, but able to capture and return the results to match

example

local s,res,res1,res2
s = 'http://www.freecls.com'

- the absence of capture, to return all matching 
- Results: HTTP: //www.freecls.com 
RES = String.match (S, ' . HTTP: //% A + \% A + \ .com ' )

- If there is capture, return the captured result 
- Results: WWW freecls 
RES1, RES2 = String.match (S, ' . HTTP: // (% A +) \ (% A +) \ .com ' )

string.gsub (s, pattern, repl [, n])

Used to do string replacement string, replace the optional parameter n represents the number of times the default Replace All, the replacement after return

example

local s,res,res1,res2
s = 'http://www.freecls.com'

- Results: HTTP: //test.freecls.com 
RES = string.gsub (S, ' WWW ' , ' Test ' )

- Capture Replace 
- Results: test.freecls.abc 
RES = string.gsub (S, ' . ^ HTTP: //% W + \ (% W +) \ .com $ ' , ' Test 1.abc%. ' )

- W replaced by t, but only replacing 2 
- Results: HTTP: //ttw.freecls.com 
RES = string.gsub (S, ' W ' , ' T ' , 2 )

string.gmatch (s, pattern)

Iteration match

example

local s = 'www.freecls.com'
words = {}
for w in string.gmatch(s, "%a+") do
    words[#words + 1] = w
end
--words最终结果为
--{'www','freecls','com'}

string.format (formatstring, ···)

String formatting sprintf type without nonsense language c of example to explain

local s = string.format('%d%s',123,'freecls')   --123freecls

s = string.format('%0.2f',1.234343)     --1.23(保留2位)

- turn to hexadecimal,% X uppercase hexadecimal 
local S = String.Format ( ' % X- ' , 140 )        - 8C 
local S = String.Format ( ' % X ' , 140 )        - 8C 
local S = String.Format ( ' % 04X ' , 140 )      - 008C

string.len (s)

Returns the string length


string.rep(s,n)

String n times and stitching returns


string.lower(s)

Small letter


string.upper(s)

Turn uppercase


string.reverse(s)

Reverse a string



 

Guess you like

Origin www.cnblogs.com/fw-qql/p/11039391.html