Lua basic string operations

Source: Internet
Author: User

Some common string operations are recorded below

 
Local STR = "Hello World" -- string. len can obtain the string length: Local Len = string. len (STR) print (LEN) -- string. rep returns the result STR = "AB" Local newstr = string. rep (STR, 2) print (newstr) -- string. lower converts the string to uppercase in lower case and returns a new copy str1 = "Shenqi" newstr = string. lower (str1) print (newstr) -- string. upper converts the string to lowercase in uppercase and returns a new copy str2 = "Shenqi" newstr = string. upper (str2) print (newstr)


Get Time Operation

 
Local tbcurrenttime = OS. date ("* t") for K, V in pairs (tbcurrenttime) Do print (K .. "=" .. tostring (V) End




Local STR = "Hello World" Local I, j = string. find (STR, "hello") print (I, j) -- 1 5str = "hello876323124world" Local I, j = string. find (STR, "% d +") print (I, j) -- 6 14 local substr = string. match (STR, "% d +") print (substr) -- 876323124 local I, j = string. find (STR, "% d +") substr = string. sub (STR, I, j) print (substr) -- 876323124


Replace

 
Local STR = "Hello World" -- the replacement times are not limited here. Local strresult, CNT = string. gsub (STR, "L", "O") print (strresult) print (CNT) -- start to limit the number of replicas strresult, CNT = string. gsub (STR, "L", "O", 1) print (strresult) print (CNT)





Address: http://blog.csdn.net/qqmcy/article/details/39180391

Lua basic string operations

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.