Lua string Library (string library) learning notes _lua

Source: Internet
Author: User
Tags numeric lowercase lua

One of the most powerful features of Lua is its string processing power, it supports character formatted output, extensible pattern matching lookup, and some useful character manipulations, such as query, intercept, replace, and delete string operations, which are encapsulated in a module named String.

The character index in Lua starts at 1 and the index value can be negative, which is interpreted as a backward index, starting at the end of the string.

Here is the string manipulation function provided by Lua 5.2:

Byte

The function string.byte the first character of the string into ASCII encoding by default, which is the encoding (with only one argument) for the output of the character:

Copy Code code as follows:

String.byte (s [, I [, J]])

Example:

Copy Code code as follows:

Print (String.byte ("abc"))//echo:97

Print (String.byte ("abc", 2)//echo:98

Char

function String.char is to convert an ASCII encoding to the corresponding character, using:

Copy Code code as follows:

String.char (ASC1, ...)

Example:

Copy Code code as follows:

Print (String.char)//echo A

Print (String.char,)//echo CDE

Dump

function String.dump Returns a string of functions in binary form, using:

Copy Code code as follows:

String.dump (function)

The parameter function is a Lua function:

Copy Code code as follows:

function test ()
Print ("Just a Test")
End
Print (String.dump (test))

function String.dump implements serialization of functions, which can be easily passed and invoked in other scopes. function string.dump the binary string, which can be returned directly by the load function inverse sequence.

Copy Code code as follows:

function test ()
Print ("Just a Test")
End
Local SD = string.dump (test)
Print (SD)
Local ls = load (SD)
Print (LS)
LS ()

Find

Function String.find Find the first position in the string s that matches the find character pattern:

Copy Code code as follows:

String.find (s, pattern [, init [, plain]])

If the target character pattern is found, its start and end positions are returned:

Copy Code code as follows:

Start, end = String.find ("Just a Test", "St")
Print (start, end)

If not found, returns nil:

Copy Code code as follows:

Print (String.find ("Just a Test", "dhq.me"))

Format

Function String.Format is used to format strings for output, using:

Copy Code code as follows:

String.Format (formatstring,)

The first parameter of the String.Format function is used to specify the format of the string, for example:

Copy Code code as follows:

Print (String.Format ("%s is%d", "ten", 10))

The%s in the example above represents a string, and%d represents a number, which is a formatted output symbol for LUA.

The use of the String.Format function is similar to the printf function in C, for example, it can output a specified number of digits like printf:

Copy Code code as follows:

Pi = 3.1415926
Print (String.Format ("Pi is%.2f", pi))

Lua's character formatted output symbol list:

Copy Code code as follows:

. All characters
%a Letters
%c control character
%d digits
%d Non-numeric
%l lowercase Letters
%p Punctuation Mark
%s string
%u uppercase letters
%w Alpha-Numeric
%x Hex Number
%z characters in 0

Gmatch

The function String.gmatch returns an iterative function that Karki through the function to all occurrences of a string s that appear in the specified pattern of matching patterns:

Copy Code code as follows:

String.gmatch (s, pattern)

For example, find all the words in the string s:

Copy Code code as follows:

s = "Just a test"
For W in String.gmatch (S, "%a+") does
Print (W)
End

Gsub

The function string.gsub is used for global string substitution, and characters in the string s that match pattern patterns are replaced with the values of the REPL parameter, using:

Copy Code code as follows:

String.gsub (s, pattern, repl [, N])

For example:

Copy Code code as follows:

Print (String.gsub ("Just a Test", "St", "*"))

Matching pattern patterns can be a regular:

Copy Code code as follows:

s = "num is 1234567890"
Print (String.gsub (S, "%d", "*"))

You can add an optional argument n to the end of the function to specify the number of times to replace it:

Copy Code code as follows:

s = "Sethook, setlocal, setmetatable, Setupvalue, Setuservalue"
Print (String.gsub (S, "s%a+", "s", 2))

Len

function String.len is used to return the length of the string s, using:

Copy Code code as follows:

String.len (s)

Example:

Copy Code code as follows:

Print (String.len ("ABCDEFG"))

Lower

The function string.lower is used to convert the letters in the string s to lowercase, using:

Copy Code code as follows:

String.Lower (s)

For example:

Copy Code code as follows:

Print (String.Lower ("AbCdEfG"))

Match

The function String.match is used to find the first matching value of pattern patterns in the string s, and returns a matching value, using:

Copy Code code as follows:

String.match (s, pattern [, Init])

The above parameter init is optional, representing the starting point for the lookup process, which begins with 1:

Copy Code code as follows:

Print (String.match ("Just a Test", "test")

The parameter patter can be a regular pattern:

Copy Code code as follows:

T = "Today is 2003-5-31"
Print (String.match (t, "%d+-%d+-%d+"))

Returns the entire string if pattern is empty, or returns nil if the match is not successful.

Copy Code code as follows:

Print (String.match ("Abcdabcd", "a"))

Rep

function String.rep Returns a string of duplicate (repeat) n-second character s, separated by the separator sep, using:

Copy Code code as follows:

String.rep (S, n [, Sep])

The default delimiter Sep is a null character.

Copy Code code as follows:

Print (String.rep ("Repeat", 3))

Reverse

function String.reverse is used to reverse the ordering of a string s, using:

Copy Code code as follows:

String.reverse (s)

For example:

Copy Code code as follows:

Print (String.reverse ("reverse"))

Sub

function string.sub is used to intercept a substring from the first and the J characters from the string s, using:

Copy Code code as follows:

String.sub (S, I [, j])

For example:

Copy Code code as follows:

Print (String.sub ("ABCDEFG", 2, 5))

Parameter I can be a negative number, in which case the position of the substring begins at the end of the string s:

Copy Code code as follows:

Print (String.sub ("ABCDEFG",-4,-2))

Argument end is omitted, a substring from I to the bottom of the string is returned:

Copy Code code as follows:

Print (String.sub ("ABCDEFG", 3))

Upper

The function string.upper is used to capitalize the letters in the string s, using:

Copy Code code as follows:

String.upper (s)

For example:

Copy Code code as follows:

Print (String.upper ("AbCdEfG"))

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.