Pankaj Agarwal
Pankaj Agarwal

Reputation: 11311

Increment of Alphabet in c#

I am exporting my data into Excel Using Open XML. now i want to increment of alphabet like as columns 'A1' to 'B1',...'Z1', 'AA1'.

I have assign 'A1' into variable and i want to increment alphabet to 'B1'.

Please provide any method/code through that can increment alphabet 'A1' to 'B1'..'Z1','AA1'.

Upvotes: 24

Views: 56474

Answers (6)

user11174633
user11174633

Reputation: 1

Char does not allow more than one at a time. Single inside single quotes. char a=‘A’; Char b=‘1’; As character holds one character at a time in general.

Upvotes: 0

Ankit Arya
Ankit Arya

Reputation: 51

You can use string builder to achieve this.

         int length = value.Length;
        var lastString = value[length - 1];
        if (lastString == 'Z')
        {
            if ((length - 2) >= 0)
                ++value[length - 2];
            else
                value.Append('A');

            value.Replace("Z", "A");
        }
        else
            ++value[length - 1];

Upvotes: -1

Devin
Devin

Reputation: 61

This method will give you the next column:

    private static Regex ALL_Z_REGEX = new Regex("^[zZ]+$");

    static string GetNextColumn(string currentColumn)
    {
        // AZ would become BA
        char lastPosition = currentColumn[currentColumn.Length - 1];

        if (ALL_Z_REGEX.IsMatch(currentColumn))
        {
            string result = String.Empty;
            for (int i = 0; i < currentColumn.Length; i++)
                result += "A";
            return result + "A";
        }
        else if (lastPosition == 'Z')
            return GetNextColumn(currentColumn.Remove(currentColumn.Length - 1, 1)) + "A";
        else
            return currentColumn.Remove(currentColumn.Length - 1, 1) + (++lastPosition).ToString();
    }

Upvotes: 0

JDunkerley
JDunkerley

Reputation: 12495

I think these function do what you want:

    public static string IncrementXLColumn(string Address)
    {
        var parts = System.Text.RegularExpressions.Regex.Matches(Address,  @"([A-Z]+)|(\d+)");
        if (parts.Count != 2) return null;
        return incCol(parts[0].Value) + parts[1].Value;
    }

    private static string incCol(string col)
    {
        if (col == "") return "A";
        string fPart = col.Substring(0, col.Length - 1);
        char lChar = col[col.Length - 1];
        if (lChar == 'Z') return incCol(fPart) + "A";
        return fPart + ++lChar;
    }

This function would take a string of A1 to B1, Z1 to AA1 etc. Should cope with ZZ1 to AAA1 as well

Upvotes: 1

agent-j
agent-j

Reputation: 27923

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);
}

Upvotes: 4

Ken D
Ken D

Reputation: 5968

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

Upvotes: 60

Related Questions