Lua String Library

Source: Internet
Author: User
Tags alphabetic character character set control characters numeric lowercase lua
The string index in--lua is,...... from the back to the next, from the -1,-2 to the front. All function in the--string library does not manipulate the string directly, returning only one result. --------------------------------------------------------------------------------------------------"Basic Functions"

Function Describe Example Results
Len Calculating string Lengths String.len ("ABCD") 4
Rep Returns n copies of a string s String.rep ("ABCD", 2) Abcdabcd
Lower Returns all uppercase letters in a string String.Lower ("AbcD") Abcd
Upper Return string All letters lowercase String.upper ("AbcD") Abcd
Format Returns a formatted string similar to printf String.Format ("The Value is:%d", 4) The value Is:4
Sub Returns substring from index I to J of S String.sub ("ABCD", 2) Bcd
String.sub ("ABCD",-2) Cd
String.sub ("ABCD", 2,-2) Bc
String.sub ("ABCD", 2, 3) Bc
Find Find in string String.find ("CDCDCDCD", "AB") Nil
String.find ("CDCDCDCD", "CD") 1 2
String.find ("CDCDCDCD", "CD", 7) 7 8
Gsub Replace in string String.gsub ("Abcdabcd", "a", "Z"); ZBCDZBCD 2
String.gsub ("AAAA", "a", "Z", 3); Zzza 3
Byte Returns the integer form of the character String.byte ("ABCD", 4) 68
Char Converts an integer number to a character and connects String.char (97,98,99,100) Abcd
--------------------------------------------------------------------------------------------------"Basic mode string"
Character class Describe Example Results
. Any character String.find ("", ".") Nil
%s whitespace characters String.find ("AB CD", "%s%s") 3 4
%s Non-whitespace characters String.find ("AB CD", "%S%S") 1 2
%p Punctuation character String.find ("Ab,.cd", "%p%p") 3 4
%P Non-punctuation characters String.find ("Ab,.cd", "%p%p") 1 2
%c Control characters String.find ("abcd\t\n", "%c%c") 5 6
%c Non-control characters String.find ("\t\nabcd", "%c%c") 3 4
%d Digital String.find ("Abcd12", "%d%d") 5 6
%d Non-digital String.find ("12abcd", "%d%d") 3 4
%x hexadecimal digits String.find ("Efgh", "%x%x") 1 2
%x Non-hexadecimal digits String.find ("Efgh", "%x%x") 3 4
%a Letters String.find ("AB12", "%a%a") 1 2
%A Non-letter String.find ("AB12", "%a%a") 3 4
%l lowercase letters String.find ("ABab", "%l%l") 3 4
%l Capital String.find ("ABab", "%l%l") 1 2
%u Capital String.find ("ABab", "%u%u") 1 2
%u Non-capital letters String.find ("ABab", "%u%u") 3 4
%w Letters and Numbers String.find ("A1 ()", "%w%w") 1 2
%W Non-letter non-numeric String.find ("A1 ()", "%w%w") 3 4
--------------------------------------------------------------------------------------------------"Escape character%"
Character class Describe Example Results
% Escape character String.find ("abc%..", "%") 4 4
String.find ("ABC.. D ","%.%. ") 4 5
--------------------------------------------------------------------------------------------------"Create a character set with []"- "is a hyphen," ^ "represents a complement to a set of characters"
Character class Describe Example Results
[01] Match binary number String.find ("32123", "[01]") 3 3
[AB] [CD] Match AC, AD, BC, BD String.find ("ABCDEF", "[AB][CD]") 2 3
[[]] Match an opposing bracket [] String.find ("Abc[]d", "[[]]") 4 5
[1-3] Match number 1-3 String.find ("312", "[1-3][1-3][1-3]") 1 3
[B-d] Match Letter B-d String.find ("DBC", "[b-d][b-d][b-d]") 1 3
[^%s] Match any non-null character String.find ("A", "[^%s]") 3 3
[^%d] Match any non-numeric character String.find ("123a", "[^%d]") 4 4
[^%a] Match any non-alphabetic character String.find ("ABC1", "[^%a]") 4 4
--------------------------------------------------------------------------------------------------"Capture" with "()"
Character class Describe Example Results
() Capturing strings String.find ("12ab", "(%a%a)") 3 4 AB
String.find ("Ab12", "(%d%d)") 3 4 12
--------------------------------------------------------------------------------------------------

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.