Page 2 of 2

Re: init script?

Posted: Mon Nov 02, 2015 7:03 pm
by ZodaInk
Shadowblitz16 wrote:is there a function that splits the string between the nth char?
and supports negative values?
Yes, no. You can split something by the nth character with string.sub().
You can make a simple function that supports negative values.

Code: Select all

function splitTheNthChar(str, i)
  if i > 0 then
    return string.sub(str,1,i)
  elseif i < 0 then
    return string.reverse(string.sub(string.reverse(str),1,-i))
  end
end
Taking your string "12,3,68" as an example. The function returns "12" if i is 2, if i is -2 then the function returns "68".
In other words:

Code: Select all

str = "12,3,68"
print(splitTheNthChar(str, 2))--prints "12"
print(splitTheNthChar(str, -2))--prints "68"
In Lua 1 is the first string, not 0.