Use the Split function to split a string in Lua

Source: Internet
Author: User

function Split (szfullstring, szseparator)
local nfindstartindex = 1
Local nsplitindex = 1
local Nsplitarray = {}
while true does
   local nfindlastindex = String.find (szfullstring, Szseparator, Nfindstartindex)
   if not Nfindlastindex then
    nsplitarray[nsplitindex] = string.sub (szfullstring, Nfindstartindex, String.len ( szfullstring))
    break
   end
   Nsplitarray[nsplitindex] = string.sub (szfullstring, Nfindstartindex, nFindLastIndex-1)
   Nfindstartindex = Nfindlastindex + String.len (szseparator)
   Nsplitindex = Nsplitindex + 1< C12/>end
return Nsplitarray
End


Usage:

Local List = Split ("abc,123,345", ",")

And then the list is inside.

Abc
123
345

The The second argument can be more than one character, but it cannot be a LUA regular expression. For example. , or%w or something like that.

The enhanced version will be released later, this should be mostly enough.

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.