I need to split a string and store it in an array. here i used string.gmatch method, and its splitting the characters exactly, but my problem is how to store in an array ? here is my script. my sample string format : touchedSpriteName = Sprite,10,rose
objProp = {}
for key, value in string.gmatch(touchedSpriteName,"%w+") do
objProp[key] = value
print ( objProp[2] )
end
if i print(objProp) its giving exact values.
The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.
A very simple example of a split function in Lua is to make use of the gmatch() function and then pass a pattern which we want to separate the string upon.
Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.
Another important function of the Lua's string library is the string. sub() function. The string. sub() function is used to extract a piece of the string.
Unfortunately, in LUA, there is no inbuilt string splitting function, which is very inconvenient. However, you could use string.gmatch to begin a regular expression matching and the following is a short and neat alternative. A string splitting function will split a source string according to a delimiter (char separator), into a array of substrings.
Definition of Lua Array An array is used to store the elements in Lua, array represents the ordered set of an object that we store inside it. Array can be one dimensional or multi-dimensional in Lua.
A string splitting function will split a source string according to a delimiter (char separator), into a array of substrings. This usually requires string parsing. Unlike other programming language, the string concatenation is made possible by double dots “..”. loading...
If we have created a one-dimensional array then it will contain several rows inside it, on the other hand on the creation of a multi-dimensional array will contain multiple columns and rows. Array in Lua uses indexing table, size of the array in Lua is not fixed can be changed or grow at runtime or based on the requirement.
Your expression returns only one value. Your words will end up in keys, and values will remain empty. You should rewrite the loop to iterate over one item, like this:
objProp = { }
touchedSpriteName = "touchedSpriteName = Sprite,10,rose"
index = 1
for value in string.gmatch(touchedSpriteName, "%w+") do
objProp[index] = value
index = index + 1
end
print(objProp[2])
This prints Sprite
(link to demo on ideone).
Here's a nice function that explodes a string into an array. (Arguments are divider
and string
)
-- Source: http://lua-users.org/wiki/MakingLuaLikePhp
-- Credit: http://richard.warburton.it/
function explode(div,str)
if (div=='') then return false end
local pos,arr = 0,{}
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1))
pos = sp + 1
end
table.insert(arr,string.sub(str,pos))
return arr
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With