I have a question about iterate through the Alphabet. I would like to have a loop that begins with "a" and ends with "z". After that, the loop begins "aa" and count to "az". after that begins with "ba" up to "bz" and so on...
Anybody know some solution?
Thanks
EDIT: I forgot that I give a char "a" to the function then the function must return b. if u give "bnc" then the function must return "bnd"
Here is something I had cooked up that may be similar. I was experimenting with iteration counts in order to design a numbering schema that was as small as possible, yet gave me enough uniqueness.
I knew that each time a added an Alpha character, it would increase the possibilities 26x but I wasn't sure how many letters, numbers, or the pattern I wanted to use.
That lead me to the code below. Basically you pass it an AlphaNumber string, and every position that has a Letter, would eventually increment to "z\Z" and every position that had a Number, would eventually increment to "9".
So you can call it 1 of two ways..
(For me, I was doing something like "1AA000")
This is like displaying an int, only using base 26 in stead of base 10. Try the following algorithm to find the nth entry of the array
Of course, if you want the first n entries, this is not the most efficient solution. In this case, try something like daniel's solution.
I know there are plenty of answers here, and one's been accepted, but IMO they all make it harder than it needs to be. I think the following is simpler and cleaner:
Note that this doesn't do any input validation. If you don't trust your callers, you should add an
IsNullOrEmpty
check at the beginning, and ac[i] >= 'A' && c[i] <= 'Z' || c[i] >= 'a' && c[i] <= 'z'
check at the top of the loop. Or just leave it be and let it be GIGO.You may also find use for these companion functions:
These two functions are zero-based. That is, "A" = 0, "Z" = 25, "AA" = 26, etc. To make them one-based (like Excel's COM interface), remove the line above the commented line in each function, and uncomment those lines.
As with the
NextColumn
function, these functions don't validate their inputs. Both with give you garbage if that's what they get.I gave this a go and came up with this:
The following populates a list with the required strings:
Here’s what I came up with.