English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A string or string (String) is a sequence of characters consisting of numbers, letters, and underscores.
In Lua, strings can be represented in the following three ways:
A sequence of characters between single quotes.
A sequence of characters between double quotes.
[[ With ]] is a sequence of characters.
The following are examples of the three ways of string representation:
string1 = "Lua" print("\"string 1 Is\"",string1) string2 = 'oldtoolbag.com' print("string 2 Is",string2) string3 = [["Lua Tutorial"]] print("string 3 Is",string3)
The output result of the above code is as follows:
"String" 1 Is" Lua 字符串 2 Is oldtoolbag.com 字符串 3 Is "Lua Tutorial"
Escape characters are used to represent characters that cannot be displayed directly, such as the backspace key, enter key, etc. For example, double quotes in a string can be represented as "\"".
All escape characters and their meanings:
Escape character | Meaning | ASCII code value (decimal) |
\a | Bell (BEL) | 007 |
\b | Backspace (BS), moves the current position to the previous column | 008 |
\f | Form Feed (FF), moves the current position to the beginning of the next page | 012 |
\n | Line Feed (LF), moves the current position to the beginning of the next line | 010 |
\r | Carriage Return (CR), moves the current position to the beginning of the line | 013 |
\t | Horizontal Tabulation (HT) (Jump to the next TAB position) | 009 |
\v | Vertical Tabulation (VT) | 011 |
\\ | Represents a backslash character ''\' | 092 |
\' | Represents a single quote character | 039 |
\" | Represents a double quote character | 034 |
\0 | Null character (NULL) | 000 |
\ddd | 1to3Any character represented by an octal number | Three-digit octal |
\xhh | 1to2Any character represented by a two-digit hexadecimal number | Two-digit hexadecimal |
Lua provides many methods to support string operations:
Serial Number | Method & Purpose |
---|---|
1 | string.upper(argument): Convert the entire string to uppercase letters. |
2 | string.lower(argument): Convert the entire string to lowercase letters. |
3 | string.gsub(mainString,findString,replaceString,num) Replace in the string. mainString is the string to be operated on, findString is the character to be replaced, replaceString is the character to replace, num is the number of replacements (can be ignored, then replace all), such as:> string.gsub("aaaa","a","z",3); zzza 3 |
4 | string.find (str, substr, [init, [end]]) Search for the specified content in a specified target string (the third parameter is the index), return its specific position. Returns nil if not found. > string.find("Hello Lua user", "Lua", 1) 7 9 |
5 | string.reverse(arg) Reverse the string. > string.reverse("Lua") auL |
6 | string.format(...) Returns a formatted string similar to printf. > string.format("the value is:%d",4) The value is:4 |
7 | string.char(arg) and string.byte(arg[,int]) char converts an integer number to a character and concatenates it, byte converts a character to an integer value (can specify a character, default is the first character). > string.char(97,98,99,100) abcd > string.byte("ABCD",4) 68 > string.byte("ABCD") 65 > |
8 | string.len(arg) Calculate the length of the string. string.len("abc") 3 |
9 | string.rep(string, n) Returns the n copies of the string string. > string.rep("abcd",2) abcdabcd |
10 | .. 链接两个字符串 > print("www.w3codebox.".."com" www.oldtoolbag.com |
11 | string.gmatch(str, pattern) 回一个迭代器函数,每一次调用这个函数,返回一个在字符串 str 找到的下一个符合 pattern 描述的子串。如果参数 pattern 描述的字符串没有找到,迭代函数返回nil。 > for word in string.gmatch("Hello Lua user", "%a+") do print(word) end Hello Lua user |
12 | string.match(str, pattern, init) string.match()只寻找源字串str中的第一个配对. 参数init可选, 指定搜寻过程的起点, 默认为1. 在成功配对时, 函数将返回配对表达式中的所有捕获结果; 如果没有设置捕获标记, 则返回整个配对字符串. 当没有成功的配对时, 返回nil。 > = string.match("I have 2 questions for you., "%d+ %a+") 2 questions > = string.format("%d, %q", string.match("I have 2 questions for you., "(%d+) (%a+)")) 2, "questions" |
字符串截取使用 sub() 方法。
string.sub() 用于截取字符串,原型为:
string.sub(s, i[, j])
参数说明:
s:要截取的字符串。
i:截取开始位置。
j:截取结束位置,默认为 -1,最后一个字符。
-- 字符串 local sourcestr = "prefix--w3codeboxgoogletaobao--suffix" print("\n原始字符串", string.format("%q", sourcestr)) -- 截取部分,第1个到第15截取最后 local first_sub = string.sub(sourcestr, 4, 15) print("\n第一次截取", string.format("%q", first_sub)) -- 取字符串前缀,第1个到第8截取最后 local second_sub = string.sub(sourcestr, 1, 8) print("\n第二次截取", string.format("%q", second_sub)) -- print("\nThe second cutout", string.format("%q", second_sub))10截取最后 local third_sub = string.sub(sourcestr, -10) print("\nThe third cutout", string.format("%q", third_sub)) -- Index out of range, output the original string local fourth_sub = string.sub(sourcestr, -100) print("\nThe fourth cutout", string.format("%q", fourth_sub))
The result of the above code execution is:
Original string "prefix--w3codeboxgoogletaobao--suffix" The first cutout "fix--w3codeboxg" The second cutout "prefix--" The third cutout "ao--suffix" The fourth cutout "prefix--w3codeboxgoogletaobao--suffix"
The following examples demonstrate how to convert the case of strings:
string1 = "Lua"; print(string.upper(string1)) print(string.lower(string1))
The result of the above code execution is:
LUA lua
The following examples demonstrate how to perform string search and reverse operations:
string = "Lua Tutorial" -- Find string print(string.find(string, "Tutorial")) reversedString = string.reverse(string) print("The new string is", reversedString)
The result of the above code execution is:
5 12 The new string is lairotuT auL
Lua provides the string.format() function to generate strings with a specific format, where the first parameter is the format, followed by various data corresponding to each code in the format.
Due to the presence of the format string, the readability of the generated long string is greatly improved. The format of this function is very similar to the printf() in C language.
The following examples demonstrate how to perform string formatting operations:
The format string may contain the following escape codes:
%c - Accept a number and convert it to the corresponding character in the ASCII code table
%d, %i - Accept a number and convert it to signed integer format
%o - Accept a number and convert it to octal number format
%u - Accepts a number and converts it into an unsigned integer format
%x - Accepts a number and converts it into a hexadecimal number format, using lowercase letter
%X - Accepts a number and converts it into a hexadecimal number format, using uppercase letter
%e - Accepts a number and converts it into a scientific notation format, using lowercase letter e
%E - Accepts a number and converts it into a scientific notation format, using uppercase letter E
%f - Accepts a number and converts it into a floating-point number format
%g(%G) - Accepts a number and converts it into the shorter format of %e(%E, corresponding to %G) and %f
%q - Accepts a string and converts it into a format that can be safely read by the Lua compiler
%s - Accepts a string and formats it according to the given parameters
To further refine the format, you can add parameters after the % sign. The parameters will be read in the following order:
(1) sign: A+) indicates that the number escape character after it will display a positive sign for positive numbers. By default, only negative numbers display a sign.
(2) sign: A
(3) placeholder: A 0, used as a placeholder when a string width is specified. The default placeholder is a space if not filled.-) alignment identifier: The default is right-aligned when the string width is specified, increase
(4) width value
(5) decimal places/String truncation: the decimal part n added after the width value, if followed by f (floating-point escape character, such as %6.3f) sets the decimal part of the floating-point number to n digits, if followed by s (string escape character, such as %5.3s) sets the string to display only the first n characters.
string1 = "Lua" string2 = "Tutorial" number1 = 10 number2 = 20 -- Basic string formatting print(string.format("Basic formatting %s %s",string1,string2)) -- Date formatting date = 2; month = 1; year = 2014 print(string.format("Date formatting %02d/%02d/%03d, date, month, year)) -- Decimal formatting print(string.format("%.",4f",1/3))
The result of the above code execution is:
Basic formatting Lua Tutorial Date formatting 02/01/2014 0.3333
Other examples:
string.format("%c", 83) -- output S string.format("%",+d", 17.0) -- Output+17 string.format("%05d", 17) -- output 00017 string.format("%o", 17) -- Output21 string.format("眻, 3.14) -- Output3 string.format("%x", 13) -- output d string.format("%X", 13) -- Output D string.format("%e", 1000) -- Output1.000000e+03 string.format("%E", 1000) -- Output1.000000E+03 string.format("%",6.3f", 13) -- Output13.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
The following examples demonstrate the conversion between characters and integers:
-- Character conversion -- Convert the first character print(string.byte("Lua")) -- Convert the third character print(string.byte("Lua",3)) -- Convert the first character from the end print(string.byte("Lua",-1)) -- Second character print(string.byte("Lua",2)) -- Convert the second character from the end print(string.byte("Lua",-2)) -- Convert integer ASCII code to character print(string.char(97))
The result of the above code execution is:
76 97 97 117 117 a
The following examples demonstrate other string operations, such as calculating string length, string concatenation, string copying, etc.:
string1 = "www." string2 = "w3codebox string3 = ".com" -- Use .. for string concatenation print("Concatenate strings", string1..string2..string3) -- String length print("String length", string.len(string2)) -- String copy 2 times repeatedString = string.rep(string2,2) print(repeatedString)
The result of the above code execution is:
Concatenate strings www.oldtoolbag.com String length 5 w3codeboxw3codebox
In Lua, matching patterns are described directly using regular strings. It is used by pattern matching functions string.find, string.gmatch, string.gsub, string.match.
You can also use character classes in the pattern string.
A character class is a pattern item that can match any character in a specific character set. For example, the character class %d Matches any number. So you can use the pattern string %d%d/%d%d/%d%d%d%d Search dd/mm/yyyy Date 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 character classes supported by Lua:
A single character (except ^$()%.[]*+-? outer): Matches the character itself
.(dot): Matches any character
%a: Matches any letter
%c: Matches any control character (such as \n)
%d: Matches any digit
%l: Matches any lowercase letter
%p: Matches any punctuation
%s: Matches any whitespace character
ᩪtches any uppercase letter
%w: Matches any letter/Number matching
%x: Matches any hexadecimal number
%z: Matches any character representing 0
%x(Here x is a non-letter non-digit character): Matches the character x. Mainly used to handle functional characters in expressions (^$()%.[]*+-?) matching problems, for example, %% matches %
[a few character classes]: Matches any character class included in []. For example, [%w_] matches any letter/Numbers, or underscore symbol (_)
[^a few character classes]: Matches any character class not included in []. For example, [^%s] matches any non-whitespace character
When the character classes are written in uppercase, it means matching with any character not in this character class. For example, %S matches any non-whitespace character. For example, '%A' matches any non-letter character:
> print(string.gsub("hello, up-down!\n","%A", ".") hello..up.down. 4
Numbers4It is not part of the string result, it is the second result returned by gsub, representing the number of replacements made.
There are some special characters in pattern matching, they have special meanings, the special characters in Lua are as follows:
( ) . % + - * ? [ ^ $
'%' is used as an escape character, so '%.' matches a dot; '%%' matches the character '%'. The escape character '%' can not only be used to escape special characters but also for all non-letter characters.
Pattern entries can be:
A single character class matches any single character in the class;
A single character class followed by a ''*' It will match zero or more characters of this class. This entry always matches the longest possible string;
A single character class followed by a ''+' It will match one or more characters of this class. This entry always matches the longest possible string;
A single character class followed by a ''-' It will match zero or more characters of this class. and*Different from This entry always matches the shortest possible string;
A single character class followed by a '? It will match zero or one character of this class. It will match one if possible;
%n, here n Can be from 1 to 9; This entry matches an equal to n The substring of the captured item (described later).
%bxy, here x and y are two clear characters; this entry matches with x start y end and among them x and y maintain balance is read means, if reading this string from left to right, each time ax then +1 when a y then -1, at the final end point y is the first one counted to 0 y. For example, the entry %b() can match bracket-balanced expressions.
%f[set], refers to boundary pattern; this entry will match a position located set a character before a character in the inside and this position does not belong to set . set set meaning as described above. The calculation of the start and end points of the matched empty string is considered to have a character is the same as '\0'.
Pattern:
Pattern refers to a sequence of pattern entries. Adding the symbol '^' at the beginning of the pattern will anchor the matching process from the beginning of the string. Adding the symbol '$' at the end of the pattern will anchor the matching process to the end of the string. If '^' and '$' appear elsewhere, they have no special meaning and only represent themselves.
Capture:
Patterns can be enclosed in parentheses to create a subpattern internally; These subpatterns are called Captures. When a match is successful, by Captures The substrings matched are saved for future use. Captures are numbered in the order of their left parentheses. For example, for the pattern "(a*(.)%w(%s*))", matches to "a"*(.)%w(%s*)" part is saved in the first capture. (Therefore, it is numbered 1 ); The character matched by "." is 2 capture number. matches to "%s"*The part following "" is 3 .
As an exception, an empty capture () will capture the current position of the string (it is a number). For example, if the pattern "()aa()" is applied to the string "flaaap", it will produce two captures: 3 and 5 .