A string or series (string) is a string of characters that consist of numbers, letters, and underscores.
Strings in the Lua language can be represented in the following three ways: a string of characters between single quotes . a string of characters between double quotes . a string of characters between [and]] .
A string instance of the above three ways is as follows:
string1 = "Lua"
print ("\" String 1 is \ "", string1)
string2 = ' runoob.com '
print ("String 2 is", string2)
string3 = [ [Lua Tutorial]]
Print ("String 3 is", string3)
The above code performs the output result:
"String 1 is" Lua
string 2 is runoob.com
string 3 is "LUA Tutorial"
The escape character is used to represent characters that cannot be displayed directly, such as the back key, the Enter, and so on. You can use "\" If you are converting double quotes in a string.
All the escape characters and the corresponding meanings:
string Manipulation
Lua provides a number of ways to support string manipulation:
string.upper (argument):
All strings are converted to uppercase letters.
string.lower (argument):
The string is all converted to lowercase letters.
string.gsub (mainstring,findstring,replacestring,num)
Replace in the string, Mainstring is the string to be replaced, FindString is the replaced character, replacestring the character to be replaced, NUM replacement number (can be ignored, replace all), such as:
> string.gsub ("AAAA", "a", "Z", 3);
Zzza 3
string.find (str, substr, [Init, [end]])
Searches for the specified content (the third parameter is an index) in a specified target string, returning its exact location. Returns nil if it does not exist.
> String.find ("Hello lua user", "Lua", 1)
7 9
String.reverse (ARG)
String inversion
> String.reverse ("Lua")
AuL
String.Format (...)
> String.Format ("The Value is:%d", 4) the
value is:4
String.char (ARG) and String.byte (Arg[,int])
Char converts an integer number to a character and joins the byte conversion character to an integer value (you can specify a character, the default first character).
> String.char (97,98,99,100)
abcd
> String.byte ("ABCD", 4)
> String.byte ("ABCD")
65
>
String.len (ARG)
Computes the string length.
String.len ("abc")
3
String.rep (String, N)
Returns n copies of String
> String.rep ("ABCD", 2)
ABCDABCD
..
Link Two strings
> Print ("Www.runoob" ...) com ")
www.runoobcom
String.gmatch (str, pattern)
Returns an iterator function that calls this function every time, returning a substring found in string str that matches the pattern description. If the string described in parameter pattern is not found, the iteration function returns nil.
For word in String.gmatch ("Hello lua user", "%a+") does
print (word)
end
Hello
Lua
User
String.match (str, pattern, init)
String.match () only looks for the first pairing in the source string str. Parameter init is optional, specifying the starting point for the search process, which defaults to 1.
When the pairing is successful, the function returns all the captured results in the pairing expression; If no capture tag is set, the entire pairing string is returned. Returns nil when there is no successful pairing.
> = String.match ("I have 2 questions for you.", "%d+%a+")
2 questions
> = String.Format ("%d,%q", string.ma TCH ("I have 2 questions for you.", "(%d+) (%a+)"))
2, "questions"
string Capitalization conversions
string1 = "Lua";
Print (String.upper (string1))
print (String.Lower (string1))
The results are:
Lua
Lua
string lookup and inversion
String = "Lua Tutorial"
-lookup string
print (String.find (String, "Tutorial")
reversedstring = String.reverse (string)
Print ("New string is", reversedstring)
Results:
5
New string for Lairotut AuL
string Formatting
Lua provides a String.Format () function to generate a string with a specific format, the first parameter of the function is formatted, followed by the various data for each code in the corresponding format.
Because of the existence of the format string, the resulting long string readability is greatly improved. This function is formatted much like printf () in the C language.
The following example demonstrates how to format a string:
The format string may contain the following escape code:
%c-accepts a number and converts it to the corresponding character%d in the ASCII table
%i-accepts a number and converts it to a signed integer format
%o-accepts a number and converts it to octal format
%u- Accepts a number and converts it to unsigned integer format
%x-takes a number and converts it to a hexadecimal number format, uses lowercase
%x-accepts a number and converts it to a hexadecimal number format, using the uppercase letter
%e- Accept a number and convert it to a scientific notation format, using the lowercase e
%E-to accept a number and convert it into a scientific notation format, using the capital letter e
%f-to accept a number and convert it to a floating-point format
%g (%g)- Accept a number and convert it to%E (%E, corresponding%g) and%f a shorter format
%q-Accept a string and convert it into a format that is safe to be read by the LUA compiler
%s-accepts a string and formats the string
according to the given parameters To further refine the format, you can add parameters after the% number. The parameters are read in the following order:
(1) symbol: A + number indicates that the following number escape character will have a positive sign. By default, only negative numbers display symbols.
(2) Placeholder: a 0 that occupies a position when the string width is specified later. The default placeholder when not filled in is a space.
(3) Alignment identification: When the string width is specified, the default is right-aligned, and the increment-number can be left-aligned.
(4) Width value
(5) Decimal digit/String trimming: The number of decimal parts added after the width value is n, if followed by f (floating-point escape character, such as%6.3f), the number of decimal points is set to keep only n digits, and if s (string escape character, such as%5.3s) is set, the string will only display the first n digits.
string1 = "Lua" string2 = "Tutorial" Number1 = ten number2 = 20--Basic string format print (String.Format ("base This format%s%s ", string1,string2)"--date format dates = 2; month = 1; Year = 2014 Print (String.Format ("date format%02d/%02d/%03d", date, month, year))--Decimal format print (String.Format ("%.4f", 1/3))
String.Format ("%c",) output S
string.format ("%+d", 17.0) output +17
string.format ("%05d", ) Output 00017
String.Format ("%o") output
String.Format ("%u", 3.14) output 3
string.format ("%x", 13) Output D
String.Format ("%x") output D
String.Format ("%e", 1000) output 1.000000e+03
String.Format ("%E", 1000) output 1.000000E+03
string.format ("%6.3f",) output 13.000 String.Format
(" %q "," one\ntwo ") output" one\
two "
String.Format ("%s "," monkey ") output Monkey String.Format
("% 10s "," monkey ") output monkey
String.Format ("%5.3s "," monkey ") output Mon
characters and integers are converted to each other
The following example shows the conversion of characters to integers:
--Character conversion-
-converting the first character
print (String.byte ("Lua"))-
-Converting the third character
print (String.byte ("Lua", 3))
-- Convert first character
print (String.byte ("Lua", -1))-
-second character
print (String.byte ("Lua", 2)-
-Convert end second character
Print (String.byte ("Lua", -2))-
-integer ASCII conversion to character
print (String.char (97))
The above code execution results are:
117
117
A
Other common functions
string1 = "www."
string2 = "Runoob"
string3 = ". com"-
-use ... for string connection
print ("Connection string", string1 ... string2.. STRING3)-
-string length
print ("String Length", String.len (string2))-
-string copy 2 times
repeatedstring = String.rep ( string2,2)
print (repeatedstring)
The above code execution results are:
Connection string www.runoob.com
string length 6runoobrunoob
Match mode
The matching pattern in Lua is described directly by a regular string. It is used for pattern matching functions string.find, String.gmatch, String.gsub, String.match.
You can also use character classes in a pattern string.
A character class refers to a pattern item that can match any character in a particular character set. For example, character class%d matches any number. So you can use the pattern string '%d%d/%d%d/%d%d%d%d ' to search for a date in dd/mm/yyyy format:
s = "Deadline is 30/05/1999, firm"
date = "%d%d/%d%d/%d%d%d%d"
print (String.sub (S, string.find (s, date)) - -> 30/05/1999
The following table lists all the character classes that LUA supports:
Single character (except ^$ ()%.[] *+-? ): Paired with the character itself
. (point): paired with any character%a: paired with any of the
letters
%c: Pairing with any of the controls (for example \ n)
%d: Paired with any number
%l: paired with any lowercase letters
%p: paired with any punctuation (punctuation)
%s: Pairing with white-space characters%u: paired with any
uppercase%w: paired with any
letter/digit%x: paired with any
hexadecimal number
%z: paired with any character that represents 0
( X is not an alphanumeric character here: paired with the character X. Used primarily to handle functional characters in an expression (^$ ()%.[] *+-?) Pairing problems, such as percent% versus%
[several character classes]: Pairing with any of the character classes contained in []. For example [%w_] paired with any letter/digit, or underscore sign (_)
: Paired with any character class that is not included in []. For example [^%s] Pairing with any non-white-space character
when the above character class is written in uppercase, the expression is paired with any character that is not a character class. For example,%s represents a pairing with any non-white-space character . For example, '%A ' non-alphabetic characters:
> Print (string.gsub ("Hello, up-down!", "%A", "."))
Hello.. Up.down. 4
The number 4 is not part of the string result, and he is the second result returned by the Gsub, representing the number of substitutions that occurred.
There are special characters in pattern matching, they have special meanings, and the special characters in Lua are as follows:
( ) . % + - * ? [ ^ $
'% ' is used as an escape character for special characters, so '%. ' matches the '% ' of the character '% '. The escape character '% ' can be used not only to escape special characters, but also for all non-alphanumeric characters.
The pattern entry can be:
A single character class matches any single character in the category
, and a single character class with a ' * ' matches 0 or more characters of that class. This entry always matches the string as long as possible; a
single character class with a ' + ' will match one or more characters of that class. This entry always matches the string as long as possible; a
single character class with a '-' will match 0 or more characters of that class. Unlike ' * ', this entry always matches as short a string as possible; a
single character class with a '? ' will match 0 or one of the characters of the class. Whenever possible, it will match one;
%n, where n can be from 1 to 9; This entry matches a substring equal to n capture (followed by a description).
%bxy, where the x and Y are two distinct characters; this entry matches the string that ends with x starting y and where x and y are balanced. Meaning, if you read this string from left to right, read one x at a time of 1, read a Y-1, The Y at the end is the first y that counts to 0. For example, an entry%b () can match an expression that is balanced by parentheses.
%f[set], refers to the border mode; This entry matches an empty string that precedes a character in the set, and the previous character of that position does not belong to set. The meaning of set set is as described earlier. The calculation of the start and end points of the string that matches is considered as having a character ' "in place".
mode:
Pattern refers to a sequence of schema entries. Precede the pattern with the symbol ' ^ ' to match the anchor from the beginning of the string. Adding the symbol "at the end of the pattern" will anchor the matching process to the ends of the string. If ' ^ ' and ' will cause the matching process to anchor to the end of the string. If ' ^ ' and ' appear in other places, they have no special meaning, they only represent themselves.
Capture:
Patterns can be internally enclosed in parentheses around a child pattern, which is called a catch. When the match succeeds, the substring in the string matched to the capture is saved for future use. The captures are numbered in the order in which they are left brackets. For example, for patterns "(A * (.) %w (%s*)) ", matching in string to" A * (.) The part of%w (%s*) is kept in the first catch (hence the number 1), and the character matched to "." is a catch of number 2nd, and the part that matches "%s*" is number 3rd.
As a special case, an empty capture () captures the position of the current string (it is a number). For example, if the Mode "() AA ()" is acting on the string "Flaaap", two catches will be generated: 3 and 5.