Does anyone know of a Javascript library (e.g. underscore, jQuery, MooTools, etc.) that offers a method of incrementing a letter?
I would like to be able to do something like:
"a"++; // would return "b"
Does anyone know of a Javascript library (e.g. underscore, jQuery, MooTools, etc.) that offers a method of incrementing a letter?
I would like to be able to do something like:
"a"++; // would return "b"
A just for laughs solution
This one does work well:
Make a function with {a: 'b', b: 'c', etc} in a closure:-
usage:-
Adding uppercase and digits:-
p.s. In some versions of Javascript, you can use
[...chars]
instead ofchars.split('')
Simple, direct solution
As others have noted, the drawback is it may not handle cases like the letter 'z' as expected. But it depends on what you want out of it. The solution above will return '{' for the character after 'z', and this is the character after 'z' in ASCII, so it could be the result you're looking for depending on what your use case is.
Unique string generator
(Updated 2019/05/09)
Since this answer has received so much visibility I've decided to expand it a bit beyond the scope of the original question to potentially help people who are stumbling on this from Google.
I find that what I often want is something that will generate sequential, unique strings in a certain character set (such as only using letters), so I've updated this answer to include a class that will do that here:
Usage:
One possible way could be as defined below