0

I am trying to append sequential characters: A, B, ... AA, AB, ... to the beginning a a stringbuilder type. The problem I am having is that it will append all ASCII characters and not double characters. My code looks like this:

string prefix = null;
System.Text.StringBuilder text = new System.Text.StringBuilder();
for (j = 0; j < genList.Count; j++)
{
    prefix = "." + Convert.ToChart(j + 65).ToString();
    text.Append(prefix + genList[j]);
}
0

1 Answer 1

2

What you really want is something that will output an integer in base-26, using the letters A through Z as digits. So 0 corresponds to A, 25 is Z, 26 is AA, etc.

const string digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

string ToBase26(int i)
{
    StringBuilder sb = new StringBuilder();
    do
    {
        sb.Append(digits[i % 26]);
        i /= 26;
    } while (i != 0);
    // The digits are backwards. Reverse them.
    return new string(sb.ToString.Reverse());
}

That's not the most optimum way to do it, but it'll work.

To output A, B, C, ... AA, AB, AC ... BA, etc:

for (int i = 0; i < Count; ++i)
{
    Console.WriteLine(ToBase26(i));
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.