Lua string
A string or string is a string of characters consisting of numbers,letters, and underscores.
Strings in the Lua language can be represented in the following three ways:
A string of characters between single quotation marks.
A string of characters between double quotation marks.
A string of characters between
[[
vs.]]
.
The string examples of the above three methods are as follows:
Example
string1 = "Lua"
print("\\"String 1 is\\"",string1)
string2 = 'runoob.com'
print("String 2 is",string2)
string3 = [["Lua tutorial"]]
print("String 3 is",string3)
The output of the above code execution is as follows:
"String 1 is" Lua
String 2 is runoob.com
String 3 is "Lua tutorial"
Escape characters are used to represent characters that cannot be displayed directly, such as back key, enter key, etc. For example, double quotation marks can be used in string conversion "\""
.
All escape characters and their corresponding meanings:
Escape character |
Meaning |
ASCII code value (decimal) |
---|---|---|
|
Ring (BEL) |
7 |
|
BS to move the current position to the previous column |
8 |
|
FF to move the current position to the beginning of the next page |
12 |
|
Line break (LF) to move the current position to the beginning of the next line |
10 |
|
Enter (CR) to move the current position to the beginning of the line |
13 |
|
Horizontal tabulation (HT) (skip to the next TAB position) |
9 |
|
Vertical tabulation (VT) |
11 |
|
Represents a backslash character |
92 |
|
Represents a single quote (apostrophe) character |
39 |
|
Represents a double quotation mark character |
34 |
|
Null character |
0 |
|
Any character represented by a 1 to 3 octal number |
Three-digit octal |
|
Any character represented by 1 to 2 digits in hexadecimal |
Binary hexadecimal system |
String operation
Lua provides a number of ways to support string manipulation:
Serial number |
Method and use |
---|---|
1 |
|
2 |
|
3 |
MainString is the string to be operated, findString is the character to be replaced, replaceString is the character to be replaced, and the number of num replacements (can be ignored, then all are replaced), such as: > string.gsub("aaaa","a","z",3);
zzza 3
|
4 |
Init specifies the starting position of the search, which defaults to 1, which can be a negative number indicating the number of characters from backto front. Plain indicates whether to use a simple pattern. By default, false,true onlydoes simple operations to find substrings, and false means to use regular pattern matching. The following example finds the start and end index positions of the string “Lua”: > string.find("Hello Lua user", "Lua", 1)
7 9
|
5 |
> string.reverse("Lua")
auL
|
6 |
> string.format("the value is:%d",4)
the value is:4
|
7 |
> string.char(97,98,99,100)
abcd
> string.byte("ABCD",4)
68
> string.byte("ABCD")
65
>
|
8 |
string.len("abc")
3
|
9 |
> string.rep("abcd",2)
abcdabcd
|
10 |
Link two strings > print("www.runoob.".."com")
www.runoob.com
|
11 |
> for word in string.gmatch("Hello Lua user", "%a+") do print(word) end
Hello
Lua
user
|
12 |
On successful pairing, the function returns all the capture results in the pairing expression, or the entire pairing string if no capture flag is set. 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.match("I have 2 questions for you.", "(%d+) (%a+)"))
2, "questions"
|
String interception
String interception using sub()
method.
string.sub()
used to intercept a string. The prototype is:
string.sub(s, i [, j])
Parameter description:
s
: The string to intercept.i
: Intercept the start positionj
: intercept the end position, default is-1, the last character.
Example
-- character string
local sourcestr = "prefix--runoobgoogletaobao--suffix"
print("\\n Original string", string.format("%q", sourcestr))
-- Intercepted parts, 4th to 15th
local first_sub = string.sub(sourcestr, 4, 15)
print("\\n First interception", string.format("%q", first_sub))
-- Prefix string, 1st to 8th
local second_sub = string.sub(sourcestr, 1, 8)
print("\\n Second interception", string.format("%q", second_sub))
-- Capture the last 10
local third_sub = string.sub(sourcestr, -10)
print("\\n Third interception", string.format("%q", third_sub))
-- Index out of bounds, output original string
local fourth_sub = string.sub(sourcestr, -100)
print("\\n Fourth interception", string.format("%q", fourth_sub))
The result of the above code execution is:
Original String "prefix--runoobgoogletaobao--suffix"
First interception "fix--runoobg"
Second interception "prefix--"
Third interception "ao--suffix"
Fourth interception "prefix--runoobgoogletaobao--suffix"
String case conversion
The following example demonstrates how to convert a string case:
Example
string1 = "Lua";
print(string.upper(string1))
print(string.lower(string1))
The result of the above code execution is:
LUA
lua
String search and inversion
The following example shows how to find and reverse a string:
Example
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
String formatting
Lua provides string.format()
function to generate a string with a specific format, the first argument to the function is the format, followed by various data for each code in the corresponding format.
Due to the existence of format strings, the readability of the resulting long strings is greatly improved. The format of this function is very similar to that of the C language printf()
.
The following example shows how to format a string:
The format string may contain the following escape codes:
% c-take a number and convert it to the corresponding characters
ASCII
inthe code table% d,% I-take a number and convert it to a signed integer format
% o-accept a number and convert it to octal format
% u-accepts a number and converts it to an unsigned integer format
% x-accept a number and convert it to hexadecimal format, using lowercase letters
% X-accept a number and convert it to hexadecimal format, using uppercase letters
% e-accept a number and convert it to scientific notation format, using the lowercase letter e
% E-accept a number and convert it to scientific notation format, using uppercase E
% f-accept a number and convert it to floating-point format
% g (% G)-takes a number and converts it to a shorter format in% 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. Parameters are read in the following order:
(1)symbol: a + sign indicates that the subsequent numeric escape character will make the positive number show the positive sign. By default, only negative numbers display symbols.
(2)placeholder: a 0 that is used when string width is specified later. The default placeholder when unfilled is a space.
(3)alignment logo: when the string width is specified, the default is rightalignment, and the plus-sign can be changed to left alignment.
Width value
(5)Decimal places/string trimming: the decimal part n that is added afterthe width value, followed by f (floating point escape character, such as
%6.3f
), the decimal of the floating point number is set to retain only n digits, followed by s (string escape character, such as%5.3s
sets the string to display only the first n bits.
Example
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:
Example
string.format("%c", 83) -- outputS
string.format("%+d", 17.0) -- output+17
string.format("%05d", 17) -- output00017
string.format("%o", 17) -- output21
string.format("%u", 3.14) -- output3
string.format("%x", 13) -- outputd
string.format("%X", 13) -- outputD
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
Conversion between characters and integers
The following example demonstrates the conversion between characters and integers:
Example
-- character conversion
-- Convert first character
print(string.byte("Lua"))
-- Convert the third character
print(string.byte("Lua",3))
-- Convert the first character at the end
print(string.byte("Lua",-1))
-- Second character
print(string.byte("Lua",2))
-- Convert the second character at the end
print(string.byte("Lua",-2))
-- Convert integer ASCII code to characters
print(string.char(97))
The result of the above code execution is:
76
97
97
117
117
a
Other common functions
The following examples demonstrate other string operations, such as calculating string length, string concatenation, string copying, and so on:
Example
string1 = "www."
string2 = "runoob"
string3 = ".com"
-- Using Make string connections
print("connection string",string1..string2..string3)
-- String Length
print("String Length ",string.len(string2))
-- Copy string twice
repeatedString = string.rep(string2,2)
print(repeatedString)
The result of the above code execution is:
connection string www.runoob.com
String Length 6
runoobrunoob
Matching pattern
The matching pattern in Lua is directly described using regular strings. It is used for pattern matching functions such as string.find
, string.gmatch
, string.gsub
, and string.match
.
You can also use character classes in pattern strings.
A character class refers to a pattern item that can match any character in aparticular character collection. For example, character classes %d
matches any number. So you can use pattern strings %d%d/%d%d/%d%d%d%d
search dd/mm/yyyy
date in format:
Example
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 supported by Lua:
Single character (except ^$()%.[]*+-?
outside): paired with the character itself
.
(dot): Pairing with any character%a
: Pair with any letter%c
: Pair with any controller (for example,n)%d
: Pair with any number%l
: Pair with any lowercase letter%p
: Pair with any punctuation punctuation%s
Pairing with white space characters%u
: Pair with any uppercase letter%w
: Paired with any letter / number%x
Paired with any hexadecimal number%z
: Pair with any character that represents 0%x
(here x is a non-alphanumeric character): paired with the character x. It is mainly used to deal with functional characters in expressions^$()%.[]*+-?
), such as %% and % pairing[数个字符类] With any
[]
The character class pairs contained in the. For example[%w_]
With any letter / number 或下划线符号(_) Pairing[^Several character classes]: Unlike any that is not included in the
[]``character class pairing in. For example ``[^%s]
pair with any non-white space character
When the above character class is written in uppercase, it is paired with any character other than this character class. For example, %S
%S
indicates pairing with any non whitespace characterfor example, '%A'
non-alphabetic characters:
> print(string.gsub("hello, up-down!", "%A", "."))
hello..up.down. 4
The number 4 is not part of the result of the string, it is gsub
, the second result returned represents the number of replacements that have occurred.
There are some special characters in pattern matching that have special meanings. The special characters in Lua are as follows:
( ) . % + - * ? [ ^ $
'%'
used as an escape character for special characters, so '%.'
matching point '%%'
matching character '%'
. Escape character '%'
it can be used not only to escape special characters, but also to all non-alphabetic characters.
Pattern entries can be:
A single character class matches any single character in that category
A single character class is compared to a
'*'
that will match zero or more characters of this class This entry always matches the string as long as possible;A single character class is compared to a
'+'
that will match one or more characters of this class This entry always matches the string as long as possible;A single character class is compared to a
'-'
that will match zero or more characters of this class, unlike'*'
, this entry always matchesthe shortest possible string;A single character class is compared to a
'?'
that will match zero or acharacter of this class Whenever possible, it will match one;%n
, where n can range from 1 to 9; this entry matches a substring equal to the n catch (described later).%bxy
, where x and y are two explicit characters; this entry matches a string that starts with x and ends with y, where x and y are balanced It means that if you read the string from left to right, it will be + 1 every time you read an x,-1 when you read a y, and the y at the end is the first yto count to 0. For example, an entry%b()
expressions that can match to parenthesis balance.%f[set]
, which refers to the border pattern; this entry matches to aset
an empty string before a character within, and the previous character in this position does not belong toset
. Setset
the meaning is as described earlier. The start and end points of the matched empty string are calculated as if there is a character ‘\0
’ there.
Mode:
A pattern refers to a sequence of pattern entries. Add a symbol to the frontof the pattern '^'
matches the anchor from the beginning of the string. Add a symbol to the end of the pattern '$'
anchors the matchingprocess to the end of the string. If '^'
and '$'
when they appearin other places, they have no special meaning, they only represent themselves.
Capture:
Patterns can enclose a subpattern internally in parentheses; these sub patterns are called catches. When the match is successful, the substringsin the string matched by the catch are saved for future use. The catches are numbered in the order of their left parenthesis. For example, for a pattern``”(a*(.)%w(%s*))”`` that matches to the "a*(.)%w(%s*)"
issaved in the first catch (and therefore number 1); by "."
the matching character is catch 2, and the part that matches to “% s*
” is number 3.
As a special case, empty capture ()
will be captured to the position ofthe current string (which is a number). For example, if you set the pattern "()aa()"
act on a string "flaaap"
two catches will be produced: 3 and 5.