0

I want to generate consecutive alphabetic strings from an integer index, which serve as automatic object names. I know how to generate base-26 from int32, which requires 7 digits. But this is not exactly what I want because for low indices I would still get 7 digits where I would like to get short names starting with a, b, c...

Suppressing leading zeros (a's) is also not an option because then the transition from one-digit to two-digit names is irregular:

a,..., z, ba, bb, bc,...,zy, zz, baa, bab,...

like in this code:

    public static string autoVarName(int i)
    {
        string result = "";

        do
        {
            int j = i % 26;
            result = (char)('a'+j) + result;
            i /= 26;
        }
        while (i>0);

        return result;
    }

What I would like to get is rather similar to how Excel generates column names:

a,..., z, aa, ab, ac,...,zy, zz, aaa, aab,...

I can't seem to be able to wrap my head around how to do this. Any search has just brought up true base-26 numbers. Does anyone know how to modify the above code to get what I want?

Ry-
  • 218,210
  • 55
  • 464
  • 476
oliver
  • 2,771
  • 15
  • 32

0 Answers0