Wintermute Engine Forum

Wintermute Engine => Technical forum => Topic started by: lacosaweb on July 30, 2009, 05:48:29 PM

Title: How to get the next char in the abc
Post by: lacosaweb on July 30, 2009, 05:48:29 PM
Hi, are any method to get the next letter in the alphabet?

I try

string="a";
string=string+1;

The result is "a1" I need "b" as a result.

Any idea? Thanks.
Title: Re: How to get the next char in the abc
Post by: Darky on July 30, 2009, 09:00:45 PM
I am experimenting with Arrays right now and you could use Arrays for this too. I'm just not sure it's very practical for whatever it is you try to achieve, but this is how it would work:

Code: [Select]
var Alphabet = new Array("a", "b", "c", "d", "e");
Game.Msg( Alphabet[0+2] );

So what you basicly do is fill an array with all the characters as values, and where ever you need it you return the Array value by its key. In this case it would return c (Array Keys always start with 0, so "a" is 0).

As said, I'm not sure its practical for you.
Title: Re: How to get the next char in the abc
Post by: MMR on July 30, 2009, 09:19:47 PM
You can use this function:

Code: [Select]
function GetNextLetter(alphabet, letter)
{
   var i=0;
   var nextLetter = "";

   for(i=0; i<alphabet.Length; i=i+1)
   {
if(alphabet[i] == letter)
{
   if(i+1 < alphabet.Length) nextLetter = alphabet[i+1];
   else nextLetter = "z";
}
   }

   return nextLetter;

}

And here you have several examples of use:

Code: [Select]
/* Example of use: */

var alphabet = new Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z");
var letter = "k";

Game.Msg(GetNextLetter(alphabet, letter)); /* It should show l */
Game.Msg(GetNextLetter(alphabet, "b"));    /* It should show c */

var other_letter = GetNextLetter(alphabet, "y");   /* other_letter should have z */

I hope this helps you!  ;)
Title: Re: How to get the next char in the abc
Post by: lacosaweb on July 31, 2009, 03:50:25 PM
Thanks for all you responses. I found another method to do this:

Code: [Select]
function Sumaletra(char)
{
var abc = new String("abcdefghijklmnopqrst");
var posicion;
posicion=abc.IndexOf(char);
char=abc.Substr(posicion+1,1);
return char;
}
Title: Re: How to get the next char in the abc
Post by: MMR on July 31, 2009, 06:24:51 PM
Yes as you can see there are several aproachs to this solution  ;)