Implementing split string dividers using Lua _lua

Source: Internet
Author: User
Tags lua

Lua is not like many other "all-inclusive" languages, including many functions, such as network communications, graphical interfaces, and so on. But Lua can easily be extended by hosting languages (usually C or C + +), which Lua can use as if it were built in. LUA consists only of a streamlined core and most basic library. This allows LUA to be small and fast to be embedded in other programs. As a result, there are no more system functions in LUA than in other languages. Accustomed to the string-splitting function of other languages, and is written on its own, recorded here, ready to use.
Below is a brief introduction to LUA:

Lua is a compact scripting language. The author is Brazilian. The language is designed to embed applications, providing flexible extensions and customization capabilities for applications.

The Lua script can be easily invoked by C + + code, or it can call C + + functions in turn, allowing Lua to be widely used in applications. Not only as an extension script, but also as a normal configuration file, instead of Xml,ini and other file formats, and easier to understand and maintain.

LUA is written by standard C and is simple and graceful, and can be compiled and run almost on all operating systems and platforms.
A complete LUA interpreter but 200k, Lua is the fastest in all of the current scripting engines. All of this determines that LUA is the best choice for embedded scripting.

Copy Code code as follows:

function Split (str, Delim, MAXNB)
--Eliminate bad cases ...
If String.find (str, delim) = = Nil Then
return {str}
End
if maxnb = = Nil or maxnb < 1 Then
MAXNB = 0--No limit
End
Local result = {}
Local Pat = "(.-)". Delim.. "()"
Local NB = 0
Local Lastpos
For part, Pos. String.gfind (STR, PAT) do
NB = NB + 1
RESULT[NB] = part
Lastpos = pos
If NB = = Maxnb then break end
End
--Handle the last field
If NB ~= MAXNB then
RESULT[NB + 1] = string.sub (str, lastpos)
End
return result
End

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.