Monday, 12 August 2013

String separation in corona sdk

String separation in corona sdk



function split(pString, pPattern)
   local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
   local fpat = "(.-)" .. pPattern
   local last_end = 1
   local s, e, cap = pString:find(fpat, 1)
   while s do
      if s ~= 1 or cap ~= "" then
         table.insert(Table,cap)
      end
      last_end = e+1
      s, e, cap = pString:find(fpat, last_end)
   end
   if last_end <= #pString then
      cap = pString:sub(last_end)
      table.insert(Table, cap)
  end
 
   return Table
end
 
--How to use Split Function
--local myDataTable = split([SomeString], [pattern])
 
local myDataTable = split("My,Sample,Wow",",")
print(#myDataTable) --Print number of entries in table
 
myDataTable = split("My-Sample-Wow-more","-")
print(#myDataTable)
 
myDataTable = split("My@Sample@Wow@more@than@you","@")
print(#myDataTable)
 
--Loop Through and print the last split data table
for i = 1,  #myDataTable do
    print(myDataTable[i])
end




output from above would be
3
4
6
My
Sample
Wow
more
than
you
 

No comments:

Post a Comment