String manipulation help

I’m wanting to use this string

"20b_1_0_-1"

And basically, return an x, y, z value, so it’s like so

x = 1
y = 0
z = -1

Note though, the first set of digits can be any length.

"2_-1_-1_-1"
"236b_0_0_0"

So it just needs to get the numbers after each _, and take into account the negative

1 Like

Im not sure what you are talking about but you’ll probably need to use string.split or string.sub

link

Use:
string.split ( string s, string separator = , )

local yourstring = “20b_1_0_-1”
local values = string.split(yourstring, “_”)
print(values[1], values[2], values[3])

That would return 20b, 1, 0, you need to start from the second thing in the table to the last because the identifier is the first thing it would put in the table, so it has to be

local coords = "20b_1_0_-1"
local values = string.split(coords, "_")
--or
local values = coords:split("_")
print(values[2], values[3], values[4]) -- 1, 0, -1

--Or if you just want the coords only, ignoring the identifier thing,

local values = string.split(coords, "_")
table.remove(values,1) --Remove first thing in table
print(values) -- {1, 0, -1}
1 Like