Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment a string with letters?

Tags:

I need to increment a string from.. let's say aaa to zzz and write every incrementation in the console (is incrementation even a word?). It would go something like this:

aaa
aab
aac
...
aaz

aba
abb
abc
...
abz

aca
acb

And so on. So far I have incremented a single letter by doing this:

String.prototype.replaceAt = function(index, character) {
    return this.substr(0, index) + character + this.substr(index+character.length);
}

string = "aaa";

string = string.replaceAt(2, String.fromCharCode(string.charCodeAt(2) + 1));

//string == "aab"

However, I am lost when it comes to the final letter being z and it should then increment letter 2 (index 1) and reset the last letter to be a.

Does anyone have or know a smart solution to this? Thanks!