Next character in the alphabet

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.
Post Reply
YuShin
Prole
Posts: 11
Joined: Mon Oct 18, 2010 10:05 am

Next character in the alphabet

Post by YuShin »

Hi,

I have a (very) basic question about Lua. I would like to get the next (or previous) of a given letter.
I have been looking around, but haven't found any answer. Is it possible?

Here is what i want to do :
I have a list of game names in an array, and a "currentGame" variable that indexes one of the games. I get the first letter of the name with something like "firstLetter = string.sub(gameName, 1, 1)", but then how do i get the ascii code (or equivalent) of this letter? With such code i could then get the next letter with "nextLetter = '\'..\code, then look in my array for a game starting with this letter.

For example, with a list like "Aero figters, altered beast, arkanoid, batsugun", and the current game being altered beast, i would like to get letter B.

I hope my english is good enough for you to understand :p

Thank you.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: Next character in the alphabet

Post by thelinx »

Code: Select all

function next_ascii(letter)
  local ascii_code = string.byte(letter)
  local next_ascii_code = ascii_code + 1
  return string.char(next_ascii_code)
end
or, this shorter version:

Code: Select all

function next_ascii(letter)
  return string.char(string.byte(letter)+1)
end
YuShin
Prole
Posts: 11
Joined: Mon Oct 18, 2010 10:05 am

Re: Next character in the alphabet

Post by YuShin »

Thank you,
I somehow failed to find the function that does like... exactly what i want :ehem:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests