string Library (string function library) in Lua summary _lua

Source: Internet
Author: User
Tags lowercase lua

The LUA interpreter has limited support for strings. A program can create strings and concatenate strings, but cannot intercept substrings, check the size of strings, and detect the contents of strings. The ability to manipulate strings in Lua basically comes from the string library.

Some of the functions in the string library are very simple:

String.len (s) returns the length of the string s;
String.rep (S, N) returns a string that repeats n times the string s; you use String.rep ("a", 2^20) to create a 1M bytes string (for example, for testing purposes);
String.Lower (s) converts uppercase letters in s to lowercase (string.upper lowercase to uppercase). You can do this if you want to not care about how the case is sorted by an array:

Copy Code code as follows:

Table.sort (A, function (a, B) return String.Lower (a) < String.Lower (b) End)

String.upper (s)Converts lowercase letters in s to uppercase
Both String.upper and string.lower are dependent on local environment variables. So, if you're in the European Latin-1 environment, the expression:
Copy Code code as follows:

String.upper ("A??") o ")-->" A?? O

The string.sub (s,i,j) function intercepts the string s from the first character to the J character. In Lua, the first character index of a string starts at 1. You can also use a negative index, which counts forward from the end of the string:-1 points to the last character,-2 to the penultimate, and so on. So, String.sub (S, 1, j) returns the prefix of J for the length of the string s; String.sub (S, J,-1) returns the suffix starting with the J character. If the 3rd argument is not supplied, the default is-1, so we write the last call as String.sub (S, j); String.sub (S, 2,-2) returns the substring after the first and last characters are removed.

Copy Code code as follows:

s = "[in brackets]"
Print (String.sub (S, 2,-2))--> in brackets

Remember: the strings in Lua are immutable. The String.sub function and other string manipulation functions in Lua do not change the value of the string, but instead return a new string. A common mistake is:

Copy Code code as follows:

String.sub (S, 2,-2)

Think the above function will change the value of the string s. If you want to modify the value of a string variable, you must assign the variable to a new string:

Copy Code code as follows:

s = String.sub (S, 2,-2)

The String.char function and the String.byte function are used to convert characters between characters and numbers. String.char gets 0 or more integers, converts each number to a character, and then returns a string of all the characters connected. String.byte (S, i) converts the first character of the string s to an integer; the second argument is optional and i=1 by default. In the following example, we assume that the character is represented in ASCII:

Copy Code code as follows:

Print (String.char)--> A
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 a String.Format () function to generate a string with a specific format, the first parameter of the function is a format (formatstring), followed by 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. Function String.Format is a powerful tool when it comes to formatting strings, especially string output. This function has two parameters, and you can use this function completely in the C language of printf. The first argument is a format string: a character that is composed of indicators and control formats. The characters in the control format after the indicator can be: decimal ' d '; hexadecimal ' x '; octal ' o '; floating point ' f '; string ' s '. There are other options available between the indicator '% ' and the control format character: Used to control more detailed formatting, such as the number of decimal digits for a floating-point:

The format string may contain the following escape code:

Copy Code code as follows:

%c-accepts a number and converts it to the corresponding character in the ASCII table
%d,%i-accepts a number and converts it to a signed integer format
%o-accepts a number and converts it to an octal number format
%u-accepts a number and converts it to an unsigned integer format
%x-accepts a number and converts it to a hexadecimal number format, using lowercase letters
%x-accepts a number and converts it to a hexadecimal number format, using uppercase letters
%e-accepts a number and converts it into a scientific notation format, using the lowercase letter E
%E-accepts a number and converts it into a scientific notation format, using the capital letter E
%f-accepts a number and converts it to a floating-point format
%g (%G)-accepts a number and converts it to a shorter form of%E (%E,%g) and%f
%q-Accepts a string and converts it to a format that can be safely read into the LUA compiler
%s-accepts a string and formats the string according to the given parameter

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 subsequent numeric escape character will have a positive number displayed plus 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.

After these parameters are the escape code types listed above (c, D, I, F, ...).

Copy Code code as follows:

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)
-->

In the first example,%.4f represents a floating-point number with 4 decimal digits after the decimal point. The second example%02d represents a decimal number with a fixed two-bit display, with insufficient front complement 0. and%2d not specified in front of 0, less than two will be filled with blank. For the format string part of the instruction Fu De detail the reference Lua manual, or refer to the C manual, because LUA invokes standard C's printf functions to achieve the final functionality.

Here are some examples:

Copy Code code as follows:

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 ("%10s", "monkey") output monkey
String.Format ("%5.3s", "monkey") output Mon

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.