The Lua interpreter has limited support for strings. A program can create and connect strings, but cannot intercept substrings, check the size of strings, and check the content of strings. The function of manipulating strings in Lua is basically from the string library.
Some functions in the string library are very simple:
String. len (s) returns the length of string s;
String. rep (s, n) returns the string s that has been repeated for n times. You use string. rep ("a", 2 ^ 20) can create a 1 M bytes string (for example, for test purposes );
String. lower (s) converts uppercase letters in s to lowercase (string. upper converts lowercase letters to uppercase letters ). If you want to sort an array regardless of Case sensitivity, you can do this:
Table. sort (a, function (a, B) return string. lower (a) <string. lower (B) end)
String. upper (s) converts lowercase letters in s to uppercase letters.
Both string. upper and string. lower depend on local environment variables. Therefore, if you are in the European Latin-1 environment, the expression:
String. upper ("?? O ") -->" ?? O"
The string. sub (s, I, j) function intercepts the string from the I character to the j character. In Lua, the index of the first character of the string starts from 1. You can also use a negative index. The negative index counts forward from the end of the string:-1 points to the last character,-2 points to the second to last, and so on. So, string. sub (s, 1, j) returns the prefix of string s whose length is j; string. sub (s, j,-1) returns the suffix starting with the j character. If no 3rd parameters are provided, the default value is-1. Therefore, we write the last call as string. sub (s, j); string. sub (s, 2,-2) returns the substring after removing the first and last characters.
S = "[in brackets]"
Print (string. sub (s, 2,-2) --> in brackets
Remember: the strings in Lua are constant. The string. sub function and other string operation functions in Lua do not change the value of the string, but return a new string. A common error is:
String. sub (s, 2,-2)
The above function will change the value of string s. If you want to modify the value of a string variable, you must assign the variable to a new string:
S = string. sub (s, 2,-2)
The string. char and string. byte functions are used to convert characters between characters and numbers. String. char returns 0 or multiple integers, converts each number to a character, and returns a string connected with all these characters. String. byte (s, I) converts the I character of string s to an integer. The second parameter is optional. By default, I = 1. In the following example, we assume that the characters are represented in ASCII:
Print (string. char (97) -->
I = 99; print (string. char (I, I + 1, I + 2) --> cde
Print (string. byte ("abc") --> 97
Print (string. byte ("abc", 2) --> 98
Print (string. byte ("abc",-1) --> 99
In the last line above, we use a negative index to access the last character of the string.
Lua provides string. format () function to generate a string with a specific format. The first parameter of the function is the format (formatstring), followed by various data of each code in the corresponding format. the existence of the format string greatly improves the readability of the generated long string. the format of this function is similar to printf () in C language (). function string. format is a powerful tool for formatting strings, especially string output. This function has two parameters. You can use this function according to the C-language printf. The first parameter is a formatting string consisting of an indicator and a character in the control format. The characters in the control format after the indicator can be: 'D' in decimal format, 'x' in hexadecimal format, 'O' in octal format, 'F' in floating point number, and 's' in string '. There are other options between the indicator '%' and the control format character: used to control more detailed formats, such as the decimal digits of a floating point:
The format string may contain the following escape codes:
% C-accept a number and convert it to the character corresponding to the ASCII code table
% D, % I-accept a number and convert it to a signed integer format
% O-accept a number and convert it to the octal number format
% U-accept a number and convert it to the 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 the scientific notation format, using the lowercase letter e
% E-accept a number and convert it to the scientific notation format, using the uppercase letter E
% F-accept a number and convert it to the floating point format
% G (% G)-accept a number and convert it to a short format in % e (% E, corresponding to % G) and % f
% Q-accept a string and convert it to a safe format read by the Lua Compiler
% S-accept a string and format it according to the given parameters
To further refine the format, you can add parameters after "%". The parameters are read in the following order:
(1) Symbol: A + sign indicates that the numeric escape character after it will display a positive number. By default, only a negative number is displayed.
(2) placeholder: A zero placeholder. It is used when the string width is specified later. If not specified, the default placeholder is a space.
(3) alignment identifier: When the string width is specified, the right alignment is used by default. You can change the minus sign to the left alignment.
(4) width value
(5) decimal places/string cropping: add n to the fractional part after the width value. If f is followed (floating point escape character, for example, % 6.3f) the decimal point of the floating point number is set to retain only n digits. If the second is followed by a string escape character, for example, % 5.3 s, the string is set to display only the first n digits.
These parameters are followed by the escape code types listed above (c, d, I, f ,...).
Print (string. format ("pi = %. 4f", PI ))
--> Pi = 3.1416
D = 5, m = 11, y = 1990
Print (string. format ("% 02d/% 02d/% 04d", d, m, y ))
--> 05/11/1990
Tag, title = "h1", "a title"
Print (string. format ("<% s> % s </% s>", tag, title, tag ))
--> <H1> a title
In the first example, %. 4f indicates a floating point number with four decimal places. In the second example, % 02d represents a fixed two-digit display in decimal number. If the number is not enough, add 0 to the front. 0 is not specified before % 2d. If there are less than two digits, it will be filled with blank space. For detailed descriptions of the format string indicators, refer to the lua manual or the C manual, because Lua calls the printf function of Standard C to implement the final function.
Here are some examples:
String. format ("% c: % c", 83) Output S
String. format ("% + d", 17.0) Output + 17
String. format ("% 05d", 17) Output 00017
String. format ("% o", 17) Output 21
String. format ("% u", 3.14) Output 3
String. format ("% x", 13) Output d
String. format ("% X", 13) Output D
String. format ("% e", 1000) Output 1.000000e + 03
String. format ("% E", 1000) Output 1.000000E + 03
String. format ("% 6.3f", 13) Output 13.000
String. format ("% q", "One \ nTwo") Output "One \
Two"
String. format ("% s", "monkey") Output monkey
String. format ("% 10 s", "monkey") Output monkey
String. format ("% 5.3 s", "monkey") Output mon