Use of the LUA string

Source: Internet
Author: User

use of the LUA string

Category: LUA2013-03-11 09:13 4023 People read Comments (0) favorite reports

Stringlua

--string.len (s) --Returns the length of the string s

--string.rep (S, N)--Returns a string that repeats n times the string s, and you 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:

--string.upper (s)--converts lowercase letters in s to uppercase

--string.sub (S,I,J)--The function intercepts the string s from the I character to the first J character. 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 a prefix of J;--string.sub (S, J,-1) returns the suffix beginning with the first J character.--If the 3rd argument is not provided, 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.

[Plain]View Plaincopyprint?
    1. s = "[in brackets]"
    2. Print (String.sub (S, 2,-2))-in brackets
    3. 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.

[Plain]View Plaincopyprint? < param name= "allowfullscreen" value= "false" >< param name= "wmode" value= "Transparent" >
    1. Print (String.char)--A
    2. i = 99;
    3. Print (String.char (i, i+1, i+2))--CDE
    4. Print (String.byte ("abc"))--97
    5. Print (String.byte ("abc", 2))--98
    6. Print (String.byte ("abc",-1))--99

The--string.format () function generates 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 strings--output. --This function has two parameters and you can use this function in the C-language printf. --The first parameter is a formatted string: consists of the indicator and the 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 '. In indicator '% ' and control format characters can also have other options: the number of decimal places used to control a more detailed format, such as a floating point:--[[%c-accepts a number and converts it to the corresponding character%d in the ASCII code table,%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 an unsigned integer format%x- Accept a number and convert it to hexadecimal number format, use lowercase%x-accept a number and convert it to hexadecimal number format, use capital letter%e-Accept a number and convert it to scientific notation format, use lowercase e%e-accept a number and convert it to scientific notation format, Use the capital Letter e%f-Accept a number and convert it to floating-point format%g (%g)-Accept a number and convert it to%E (%E, corresponding%g) and%f in a shorter format%q-accept a string and convert it to a format that is safe to read in by the LUA compiler-accept a string The string is formatted in a further refinement format according to the given parameters and can be added after the% number. The parameters are read in the following order: (1) symbol: A + sign indicates that a positive number will be displayed in the numeric escape character. 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) scale/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, 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, ...).

]]

[Plain]View Plaincopyprint?
  1. D = 5; m = 11; y = 1990
  2. Print (String.Format ("%02d/%02d/%04d", D, M, y))--%02d is represented with a fixed two-bit display of decimal numbers, with insufficient front-fill 0. And the%2d does not specify 0, less than two bits will be filled with blank
  3. -05/11/1990
  4. Tag, title = "H1", "a title"
  5. Print (String.Format ("<%s>%s</%s>", tag, title, tag))
  6. -
  7. String.Format ("%%c:%c", 83)--Output s
  8. String.Format ("%+d", 17.0)--Output +17
  9. String.Format ("%05d", 17)--Output 00017
  10. String.Format ("%o", 17)--Output 21
  11. String.Format ("%u", 3.14)--Output 3
  12. String.Format ("%x", 13)--Output D
  13. String.Format ("%x", 13)--Output D
  14. String.Format ("%e", 1000)--Output 1.000000e+03
  15. String.Format ("%E", 1000)--Output 1.000000E+03
  16. String.Format ("%6.3f", 13)--Output 13.000
  17. String.Format ("%q", "One\ntwo")--Output "one\
  18. String.Format ("%s", "Monkey")--Output Monkey
  19. String.Format ("%10s", "monkey")--Output Monkey
  20. String.Format ("%5.3s", "monkey")--Output Mon

--string.gsub (s, pattern, Reps[,limitnumber]) replaces all pattern strings in s with reps, returns the result string + matches--s source string--the character pattern to be replaced--replaced by reps-- Limitnumber limit the number of substitutions

[Plain]View Plaincopyprint?
    1. Print (String.gsub ("Hello, World", "O", "a"))--Hella, Warld 2
    2. --gsub can also use copy capture techniques
    3. Print (String.gsub ("Hello, World", "(O)", "%1-%1"))--Hello-o, Wo-orld 2
    4. Print (String.gsub ("Hello Lua", "(.) (.) ","%2%1 ")--ehll Oula 4 position swap per 2 characters
    5. function trim (s) return (String.gsub (S, "^%s* (.-)%s*$", "%1")) End--note the number of matches is discarded with parentheses

--string.gsub (S, Pattern, func)--func matching operation found, and outgoing replacement value

[Plain]View Plaincopyprint?
    1. S, n = string.gsub ("Hello World", "L +", function (s) return "XXX" end)
    2. Print (S, N)--Hexxxo Worxxxd 2

--string.gfind (S, pattern)--Returns an iterator that executes each time and returns the next matching string;

[Plain]View Plaincopyprint?
    1. iter = String.gfind ("A=b c=d", "[^%s+]=[^%s+]")
    2. Print (ITER ())--<== a=b
    3. Print (ITER ())--<== C=d

--string.match (s,d)--similar to String.find but returns a matching substring--s source string--d target string or pattern

[Plain]View Plaincopyprint?
    1. Print (String.match ("Hello World", "Hello"))--hello
    2. Local today= "Today is 19/2/2001"
    3. Print (String.match (Today, "%d+/%d+/%d"))--19/2/2001

--string.gmatch () --Returns an iterative function that iterates through the target string that appears

Use of the LUA string

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.