init script?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
ZodaInk
Prole
Posts: 18
Joined: Thu Jan 08, 2015 5:18 am

Re: init script?

Post 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.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 12 guests