1. Basic String Functions:
There are some functions in the string library that are very simple, such as:
1). String.len (s) returns the length of the string s;
2). String.rep (S,n) Returns the result of the string s repeating n times;
3). String.Lower (s) returns a copy of S, where all uppercase letters are converted in lowercase form, and the other characters are unchanged;
4). String.upper (s) and lower instead, lowercase is converted to uppercase;
5). String.sub (S,I,J) extracts the first and second characters of the string s. In Lua, the index value of the first character is 1, the last one 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, which is equivalent to the printf function in the C language, such as:
Print (String.Format ("PI =%.4f", Math.PI))--Output PI = 3.1416
7). The String.char (...) parameter is 0 to multiple integers, and each integer is converted to the corresponding character. It then returns a string that is concatenated by these characters, such as:
Print (String.char (97,98,99))--Output ABC
8). String.byte (s,i) returns the ASCII value of the I-character of the string s, and, if there is no second argument, returns the ASCII value of the first character by default.
Print (String.byte ("abc"))--Output 97
Print (String.byte ("abc",-1))--Output 99
Because a variable of the string type is a variable of immutable type, in all string-related functions, the string value in the argument cannot be changed, but a new value is generated to return.
2. Pattern matching function:
Lua's string library provides a powerful set of pattern-matching functions, such as Find, match, Gsub, and Gmatch.
1). String.find function:
Searches for a pattern in the target string, returns a matching starting and ending index if found, or nil. Such as:
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
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 the search from the target string. is used primarily to search all matching substrings in the target string, and each search starts at the location that was last found. Such as:
1 local t = {}
2 Local i = 0
3 while True
4 i = String.find (s, "\ n", i+1)
5 if i = = Nil Then
6 Break
7 End
8 t[#t + 1] = I
9 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:
The function has 3 parameters, a target string, a pattern, and a replacement string. The basic usage is to replace all occurrences of the pattern in the target string with a replacement string. Such as:
Print (String.gsub ("Lua is cute", "cute", "great"))--Output Lua is great
The function also has an optional 4th parameter, which is the number of actual substitutions.
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 also has another result, that is, the number of actual replacements.
Count = Select (2, String.gsub (str, "", ""))--the number of output str hollow lattice
4). String.gmatch function:
Returns a function through which the returned function can traverse all occurrences of a specified pattern in a string. Such as:
1 words = {}
2 s = "Hello World"
3 for W in String.gmatch (S, "%a+") do
4 Print (W)
5 words[#words + 1] = W
6 End
7-The output is:
8--hello
9--world
3. Mode:
The following list shows the schema metacharacters currently supported by LUA;
Pattern Meta characters |
Describe |
. |
All characters |
%a |
Letters |
%c |
Control characters |
%d |
Digital |
%l |
lowercase letters |
%p |
Punctuation |
%s |
White space characters |
%u |
Capital |
%w |
alphabetic and numeric characters |
%x |
hexadecimal digits |
%z |
Internally represented as 0 characters |
The uppercase forms of these metacharacters represent their complement sets, such as%a, which represent all non-alphabetic characters.
Print (String.gsub ("Hello, up-down!", "%s", ".")) --Output hello.. Up.down. 4
The 4 in the previous example indicates the number of replacements.
In addition to the above meta-characters, LUA provides several additional key characters. Such as: ().% +-*? [ ] ^ $
which%Represents an escape character, such as%. Represents a point (.), and a percent is a percentage sign (%).
Square brackets[]Indicates that different characters are categorized to create their own character classifications, such as [%w_] for matching characters, numbers, and underscores.
Line-) means to connect a range, such as [0-9a-z]
If^Characters in square brackets, such as [^\n], represent all characters except \ n, which represent a complement to the classification in square brackets. If ^ is not in square brackets, it starts with the following character,$As opposed to it, it means ending with the preceding character. For example: ^hello%d$, the matching string may be Hello1, Hello2, and so on.
There are also 4 parts in Lua that are used to modify a repeating part of a pattern, such as: + (repeat 1 or more times), * (repeat 0 or more times),-(repeat 0 or more times) and? (occurs 0 or 1 times). Such as:
Print (String.gsub ("one, and"; and three ","%a+ "," word ")--output word, word Word; Word Word
Print (String.match ("The number 1298 is even", "%d+"))--Output 1298
Asterisk*) and horizontal lines (-The main difference is that asterisks always try to match more characters, while horizontal lines always try to match the fewest characters.
4. Capturing (Capture):
The capture feature extracts the contents of the pattern from the target string according to a pattern. When you specify a capture, you should write the part of the pattern that you want to capture inside a pair of parentheses. For a pattern with capture, the function String.match returns all the captured values as separate results. That is, it will cut the target string into more than one captured part. Such as:
1 pair = "name = Anna"
2 Key,value = String.match (Pair, "(%a+)%s*=%s* (%a+)")
3 Print (key,value) -Output name Anna
5 date = "Today is 2012-01-02"
6 y,m,d = String.match (date, "(%d+) \-(%d+) \-(%d+)")
7 print (y,m,d) -output 02
You can also use capture for the pattern itself. That is, %1 represents the first capture, and so on,%0 represents the entire match, such as:
1 Print (String.gsub ("Hello Lua", "(.) (.) ","%2%1 ") --swap the adjacent two characters and output as Ehll Oula
2 Print (String.gsub ("Hello lua!", "%a", "%0-%0")) --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, and if it is a function, String.gsub will call the function each time a match is found, and the argument at the time of the call is the captured content, and the return value of the function is the string to be replaced. When invoked with a table, String.gsub uses the contents of each capture as a key, finds it in the table, and uses the corresponding value as the string to be replaced. If the table does not contain this key, then string.gsub does not change the match. Such as:
1 function expand (s)
2 return (String.gsub (S, "$ (%w+)", _g))
3 End
5 name = "Lua"; Status = "Great"
6 Print (Expand ("$name is $status, isn ' t it?")) --Output Lua is great, isn ' t it?
7 Print (Expand ("$othername is $status, isn ' t it?")) --Output $othername is great, isn ' t it?
9 function Expand2 (s)
Ten return (String.gsub (S, "$ (%w+)", function (n) return ToString (_g[n])))
One end
Print (Expand2 ("print = $print; A = $a "))--Output print = FUNCTION:002B77C0; A = Nil
Step by Step (Lua string library) (GO)