Increment of Alphabet in c#

This can be done:

char c1 = 'A';
c1++; // c1 is 'B' now

and you can add the numbering as a string, even the concatenated characters can be generated the same way:

pseudo code:

If Reached_Z Then Add_Another_A

This example uses an iterator capable of going from A through ZZ.

public static IEnumerable<string> GetColumns()
{
   string s = null;
   for (char c2 = 'A'; c2 <= 'Z' + 1; c2++)
   {
      for (char c = 'A'; c <= 'Z'; c++)
      {
         yield return s + c;
      }
      s = c2.ToString ();
   }
}

This sample starts at A1 and goes through AA1

string currentCell = "A1";
int currentRow = int.Parse(Regex.Match(currentCell, @"\d+").Value);
string currentCol = Regex.Match(currentCell, @"[A-Z]+").Value;
foreach (string column in GetColumns().Where (c => c >= currentCol && currentCol <= "AA"))
{
   Console.WriteLine (column + currentRow);
}

This sample starts at C5 and enumerates through the next 26 columns.

int columnsToAdd = 26;
currentCell = "C5";
currentRow = int.Parse(Regex.Match(currentCell, @"\d+").Value);
currentCol = Regex.Match(currentCell, @"[A-Z]+").Value;
foreach (string column in GetColumns().Where (c => c >= currentCol))
{
   if (columnsToAdd--) == 0)
      break;
   Console.WriteLine (column + currentRow);
}

Tags:

C#