Splitting error

Hello! I have problems with split strings. I’m creating string hasher, and wanna split value “30203” with string “0” (so result will be like “3”,“2”,“3”) but i’m getting: “1”. only “1”, nothing more. Only one value, and it’s “1”. How to fix it?
P.S i using str.split(str, “0”)

Are you sure?
image
what is the complete code?

Does the string actually contain a 0 in it?

local keys = string.split(key, "0")
for key in keys do
	print(key.. " - key")
end

Output:

 1 - key  -  Server

It contains “0” in it, you can check, i told you the string

Basically string.split removes characters based on whats been provided. The second argument is what is supposed to be removed. Then it finds all occurrences of that character and deletes it. The leftovers are made into a table

oops i accidentally replied to you

Gives 1 since that is the index of the table, to get the string you have to add another variable

local keys = string.split("323", "0")
for index, key in pairs(keys) do
	print(key.. " - key")
end

and only print the first value because you don’t use pairs

1 Like

for key, value in pairs(keys) do
That’s the proper way to write a for loop

ur doing str.split(str,"0") I suppose “str” is 30203 so ur doing it wrong the first argument is already given it would be.

local str = "30203"
print(str.split("0")) -- 3,2,3

That’s not actually correct unless you use method syntax, and neither of you are. You need to change your code to method syntax if you want to omit str as the first argument:
str:split(“0”)

1 Like

Most string library functions automatically coerce number values/literals into string values/literals, the following is valid.

string.split(10203, 0) --Splits number into table containing 1, 2, 3.

Can you show us how you are using the split function