Lua string library collection
1. Basic string functions:
Some functions in the string library are very simple. For example:
1). string.len(s) returns the length of string s;
2). string.rep(s,n) returns string s repeated n times The result of;
3). string.lower(s) returns a copy of s, in which all uppercase letters are converted to lowercase, and other characters remain unchanged;
4). string.upper(s) is the opposite of lower and converts lowercase to uppercase;
5). string.sub(s,i,j) extracts the ith to jth characters of the string s. In Lua, the index value of the first character is 1, the last is -1, and so on, such as:
print(string.sub(“[hello world]”,2,-2) ) – output hello world
6). string.format(s,…) returns the formatted string, and its formatting rules are equivalent to the printf function in C language, such as:
p>
print(string.format(“pi = %.4f”,math.pi)) –output pi = 3.1416
7). The parameters of string.char(…) are 0 to multiple integers, and convert each integer to the corresponding character. Then return a string formed by concatenating these characters, such as:
print(string.char(97,98,99)) –output abc
8). string .byte(s,i) returns the Ascii value of the i-th character of the string s. If there is no second parameter, the Ascii value of the first character is returned by default.
print(string.byte(“abc”)) –output 97
print(string.byte(“abc”,-1)) –output 99
Since string variables are immutable variables, in all string-related functions, the string value in the parameter cannot be changed, but a new value is generated and returned.
2. Pattern matching functions:
Lua’s string library provides a set of powerful pattern matching functions, such as find, match, gsub and gmatch .
1). string.find function:
Search for a pattern in the target string, if found, return the matching start index and end index, otherwise return nil. For example:
1 s = “hello world”
2 i, j = string.find(s,”hello”)
3 print(i, j) –output 1 5
4 i, j = string.find(s,”l”)
5 print(i, j) –output 3 3
p>
6 print(string.find(s,”lll”)) –output nil
The string.find function also has an optional parameter, which is An index that tells the function where to start searching in the target string. It is mainly used to search for all matching substrings in the target string, and each search starts from the position found last time. For example:
local t = {}
local i = 0
while true do
i = string.find(s, “\n”,i+1)
if i == nil then
break
end
t[#t + 1] = i
end
2). string.match function:
this function Returns the part of the target string that matches the pattern string. Such as:
1 date = “Today is 2012-01-01”
2 d = string.match(date,”%d+\-% d+\-%d+”)
3 print(d) – output 2012-01-01
3). string.gsub function:< /p>
This function has 3 parameters, target string, pattern and replacement string. The basic usage is to replace all occurrences of patterns in the target string with replacement strings. Such as:
print(string.gsub(“Lua is cute”,”cute”,”great”)) –output Lua is great
The function also has an optional fourth parameter, which is the number of actual replacements.
print(string.gsub(“all lii”,”l”,”x”,1)) –output axl lii
print(string.gsub(“all lii “,”l”,”x”,2)) –output axx lii
The function string.gsub returns another result, which is the number of actual replacements.
count = select(2, string.gsub(str,” “,” “)) –output the number of spaces in str
still According to this function to realize some other functions, such as split string:
string.split = function(s, p)
local rt= {}
string.gsub(s,'[^’..p..’]+’, function(w) table.insert(rt, w) end )
return rt
end
— The test code is as follows:
local str = “abc,123,hello,ok”
local list = string.split(str,’,’)
for _, s in ipairs(list) do
print(tostring(s))
end< /p>
— The output results are as follows:
abc
123
hello
ok
In addition, watching this article by Yunfeng, I found that the string.gsub function can also be used like this:
local str = “#qwe&qwe&qwe&##qwe&qwe&qwe&##qwe&qwe&qwe&##qwe&qwe&qwe&#”
str = str.rep(str, 1000)
str = string. gsub(str,’&’,’&’)
str = string.gsub(str,’#’,’@@@’)
str = string.gsub (str,’q’,’Q’)
str = string.gsub(str,’#’,’BB’)
print(str)
< p>
The above implementation can also be written like this~ as follows:
local escapeAtt rib = {
[‘&’] =’&’;
[‘#’] =’@@@’;
[‘q ‘] =’Q’;
[‘e’] =’BB’;
}
local str = ” #qwe&qwe&qwe&##qwe&qwe&qwe&##qwe&qwe&qwe&##qwe&qwe&qwe&#”
str = str.rep(str, 1000)
str = string.gsub(str,'[&#qe ]’, escapeAttrib)
print(str)
I ran many times with the SciTe that comes with lua, and the former performance will be slightly better by visual inspection;< /p>
4). string.gmatch function:
Return a function, through this returned function, you can traverse to all the places where the specified pattern appears in a string . Such as:
words = {}
s = “hello world”
for w in string.gmatch(s,” % a + “) do print (” —- “) p>
print (w) p>
words [#words + 1] = w p>
end < / p>
– The output is: p>
—- p>
hello —- world p>
p>
3. Mode:
The following list shows the mode metacharacters currently supported by Lua;
Mode metacharacter description
. All characters
%a Letters
%c Control characters
%d Numbers
%l Lowercase letters
%p Punctuation marks< /p>
%s blank characters
%u uppercase letters
%w letters and numbers characters
%x hexadecimal numbers
p>
%z Characters that are internally represented as 0
The uppercase form of these metacharacters indicates their complement, such as %A, which means all non-letter characters.
print(string.gsub(“hello, up-down!”,”%S”,”.”)) – output hello..up.down. 4
The 4 in the above example represents the number of replacements.
In addition to the above metacharacters, Lua also provides several other key characters. For example: ().% +-*? [] ^ $
where% represents an escape character, such as %. represents a dot (.) and %% represents a percent sign (%).
Square brackets [] indicate that by classifying different characters, you can create your own character classification. For example, [%w_] indicates matching characters, numbers, and underscores.
The horizontal line (-) means to connect a range, such as [0-9A-Z]
If the ^ character is in square brackets, such as [^\n], it means to divide \ All characters except n represent the complement of the category in square brackets. If ^ is not in square brackets, it means it starts with the following character, and $ is the opposite of it, which means it ends with the preceding character. For example: ^Hello%d$, the matched string may be Hello1, Hello2, etc.
Lua also provides 4 types of repetitive parts to modify the pattern, such as: + (repeat 1 or more times), * (repeat 0 or more times),-(repeat 0 Times or more) and? (Occurs 0 or 1 time). Such as:
print(string.gsub(“one, and two; and three”,”%a+”,”word”)) –output word, word word; word word
< p> print(string.match(“the number 1298 is even”,”%d+”)) –output 1298
The main difference between the asterisk (*) and the horizontal line (-) is that the star The sign always tries to match more characters, while the dash always tries to match the fewest characters.
The format string may contain the following escape codes:
%c-accepts a number and converts it Is the corresponding character in the ASCII code table
%d, %i-accepts a number and converts it into a signed integer format
%o-accepts a number and converts it Convert to octal number format
%u-accept a number and convert it to unsigned integer format
%x-accept a number and convert it to hexadecimal number Format, use lowercase letters
%X-accept a number and convert it to hexadecimal format, use uppercase letters
%e-accept a number and convert it For scientific notation format, use lowercase letter e
%E-accept a number and convert it to scientific notation format, use uppercase letter E
%f-accept A number and convert it to floating point format
%g(%G)-accept a number and convert it to the shorter of %e (%E, corresponding to %G) and %f Various formats
%q-accept a string and convert it into a format that can be safely read by the Lua compiler (that is, the string is added with “”)
%s- Accept a string and format the string according to the given parameters
For further refinement of the format, you can add parameters after the% sign. The parameters will be read in the following order Enter:
(1) Symbol: A + sign indicates that the following numeric escape character will make positive numbers display positive signs. By default, only negative numbers display signs.
p>
(2) Placeholder: A 0, which is used when the width of the string is specified later. The default placeholder when it is not filled in is a space.
(3) Alignment mark : When the width of the string is specified, the default is right-aligned, and the-sign can be changed to left-aligned.
(4) Width value
(5) Decimal places/word String cutting: the fractional part n added after the width value, if followed by f (floating point escape character, such as %6.3f), set the floating point number to retain only n decimal places, if followed by s (string Escape character, such as %5.3s), set the string to display only the first n digits.
4. Capture:
Capture The function can extract content that matches the pattern from the target string based on a pattern. When specifying capture, the part that needs to be captured in the pattern should be written in a pair of parentheses. For patterns with captures, the function string.match will return all captured values as separate results. That is, it will cut the target string into multiple captured parts. Such as:
pair = “name = Anna”
key,value = string.match(pair,”(%a+)%s*=% s*(%a+)”)
print(key,value) – output name anna
date = “Today is 2012-01-02 “
y,m,d = string.match(date,”(%d+)\-(%d+)\-(%d+)”)
print(y, m,d) –output 2012 01 02
You can also use capture on the pattern itself. That is, %1 means the first capture, and so on, %0 means the entire match, such as:
1 print(string.gsub(“hello Lua”,”(. )(.)”,”%2%1″)) –Swap two adjacent characters and output as ehll ouLa
2 print(string.gsub(“hello Lua!”,” %a”,”%0-%0″)) –The output is h-he-el-ll-lo-o L-Lu-ua-a!
5. Replace:
The third parameter of the string.gsub function can be not only a string, but also a function or table. If it is a function, string.gsub will call the function every time a match is found , The parameter in the call is the captured content, and the return value of the function is the string to be replaced. When called with a table, string.gsub will use the captured content each time as the key, search in the table, and use the corresponding value as the string to be replaced. If the key is not included in the table, then string.gsub does not change the match. Such as:
function expand(s)
return (string.gsub(s,”$(%w+)”,_G))
end
name = “Lua”; status = “great”
print(expand(“$name is $status, isn’ t it?”)) –Output Lua is great, isn’t it?
print(expand(“$othername is $status, isn’t it?”)) –Output $othername is great, isn’t it?
function expand2(s)
return (string.gsub(s,”$(%w+)”,function (n) return tostring(_G[n]) end))
end
print(expand2(“print = $print; a = $a “)) – output print = function: 002B77C0; a = nil
In addition: attached to the Internet about the conversion of Lua’s table and String The method is as follows:
— Serialize tablle table–convert the table into string
function serialize(obj)
local lua = “”
local t = type(obj)
if t == “number” then
lua = lua .. obj
elseif t == “boolean” then
lua = lua .. tostring(obj)
elseif t == “string” then
lua = lua .. string.format (“%q”, obj)
elseif t == “table” the n
lua = lua .. “{\n”
for k, v in pairs(obj) do
lua = lua .. “[” .. serialize(k) .. “]=” .. serialize(v) .. “,\n”
end
local metatable = getmetatable(obj)
if metatable ~= nil and type(metatable.__index) == “table” then
for k, v in pairs(metatable.__index) do
lua = lua .. “[” .. serialize(k) .. “]=” .. serialize(v) .. “,\n”
end
end
lua = lua .. “}”
elseif t == “nil” then
return nil
else
Return “-nil-“
–error(“can not serialize a “.. t ..” type.”)
end
return lua
end
— Deserialize tablle table–convert string into table
function unserialize(lua)< /p>
local t = type(lua)
if t == “nil” or lua == “” then
return nil
elseif t == “number” or t == “string” or t == “boolean” then
lua = tostrin g(lua)
else
error(“can not unserialize a “.. t ..” type.”)
end
< p> lua = “return “.. lua
local func = loadstring(lua)
if func == nil then
return nil
end
return func()
end