Library of String Functions in Lua

Source: Internet
Author: User
Tags lowercase lua
Library of String functions in 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 a string, and detect the contents of a string. The ability to manipulate strings in Lua is essentially 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 can use String.rep ("a", 2^20) to create a 1M bytes (for example, to test the need);
String.Lower (s) converts uppercase letters in s to lowercase (string.upper converts lowercase to uppercase). If you want to sort an array without caring about the case, 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
Both String.upper and String.Lower rely on local environment variables. So, if you're in the European Latin-1 environment, the expression:
String.upper ("A?? O "and" A?? O
The string.sub (s,i,j) function intercepts the string s from the i-th character to the J-character strings. In Lua, the first character index of a string starts at 1. You can also use negative indexes, and negative indexes count forward from the end of a string: 1 points to the last character, 2 points to the penultimate, and so on. So, String.sub (S, 1, j) returns the length of the string s as the prefix of J; String.sub (S, J,-1) returns the suffix starting with the first J character. If you do not provide a 3rd parameter, 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.
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 manipulation functions in Lua do not change the value of the string, but instead return a new string. A common mistake is:
String.sub (S, 2,-2)
Think that 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:
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 that all of these characters join together. String.byte (S, i) converts the first character of the string s to an integer, and the second parameter is optional, i=1 by default. In the following example, we assume that the characters are expressed in ASCII:

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 the format (formatstring), followed by a variety of data for each designator in the corresponding format. Because of the existence of the format string, the resulting long string readability is greatly improved. The format of this function is much like printf () in C. The function String.Format is a powerful tool when it is used to format strings, especially string output. This function has two parameters and you can use this function in the C-language printf. The first argument is a formatted string: consists of an indicator and a character that controls the format. The character of the control format after the indicator can be: decimal ' d '; hexadecimal ' x '; octal ' o '; floating-point ' f '; string ' s '. There are other options between the indicator '% ' and the control format character: the number of digits used to control a more detailed format, such as a decimal point of a float:

The format string may contain the following escape codes:

%c-accepts a number and converts it to the corresponding character in the ASCII code table
%d,%i-accepts a number and converts it to a signed integer format
%o-accepts a number and converts it to octal number format
%u-accepts a number and converts it to 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 to 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 translates it into a shorter format in%E (%E, corresponding to%g) and%f
%q-accepts a string and translates it into a format that is safe to read into by 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 + sign indicates that the following numeric escape character will have positive numbers displayed. By default, only negative numbers display the symbol.
(2) Placeholder: a 0 that occupies a placeholder when the string width is specified later. The default placeholder when not filled is a space.
(3) Alignment identification: When a string width is specified, the default is right-justified, and the increment-number can be left-aligned.
(4) Width value
(5) Decimal digit/String trimming: The number of decimal points added after the width value is n, if followed by f (floating-point escape character, such as%6.3f), sets the number of the float to only n bits, and if followed by S (String escape character, such as%5.3s), sets the string to display only the first n bits.

Following these parameters is the type of escape code 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))
-
In the first example,%.4f represents a floating-point number with 4 decimal places after the decimal point. The second example%02d represents a fixed two-bit display in decimal numbers, with insufficient front-fill 0. %2D is not specified in front of 0, less than two will be filled with blanks. For the format string section indicate Fu De a detailed description of the clear reference Lua manual, or refer to the C manual, because LUA invokes standard C's printf function to achieve 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\
"
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.