Last index of string.split()?

How do I get the last index of string.split()?
Lets say i have this string “1/2/3/4/5” and i split at the / how do i get the last index (in this case 5)?
Ive looked around the DevForum but found nothing that helped me.

You can do string.split("1/2/3/4/5","/")[#string.split("1/2/3/4/5","/")]

1 Like

ok thanks but I just realized that i need the index before that so (4) and not the last one

string.split returns an array of all the split strings so you can just index whichever element directly if it doesn’t change (arr[4]) or iterate the array and find the index that contains “4”

local numbers = string.split("1/2/3/4/5", "/")

local four = numbers[4]

print(four) --> 4

yes i know that but the string that im trying to split changes in length

i tryed to loop through the array and then remove 1 and print that but that just gives me nil

I don’t understand what you’re trying to do, you want to get the second last index of an array?

You can do string.split("1/2/3/4/5","/")[#string.split("1/2/3/4/5","/")-1] instead

1 Like

that just gives me a error
attempt to perform arithmetic (sub) on string and number

My bad, i edited the reply. Maybe that will work

Then you can just do

string.split("1/2/3/4/5","/")[#string.split("1/2/3/4/5","/")-1] -- subtract 1 from table length

(whoops, didn’t realise someone already gave the exact same solution)

1 Like

that worked, thanks for the help