Created
December 30, 2013 16:22
-
-
Save txusko/0b20e07d7f076614e01c to your computer and use it in GitHub Desktop.
Incrementa el string tcCodigo
Ej. string lastCodigo = IncrementXLColumn("AA"); -> Devolvera lastCodigo = "AB";
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Incrementa el string tcCodigo | |
/// Ej. string lastCodigo = IncrementXLColumn("AA"); -> Devolvera lastCodigo = "AB"; | |
/// </summary> | |
/// <param name="tcCodigo"></param> | |
/// <returns></returns> | |
protected static string IncrementXLColumn(string tcCodigo) | |
{ | |
//var parts = System.Text.RegularExpressions.Regex.Matches(Address, @"([A-Z]+)|(\d+)"); | |
var lParts = System.Text.RegularExpressions.Regex.Matches(tcCodigo, @"([A-Z])|([A-Z])"); | |
if (lParts.Count != 2) return null; | |
if (lParts[1].Value == "Z") return incCol(lParts[0].Value) + "A"; | |
else return lParts[0].Value + incCol(lParts[1].Value); | |
} | |
private static string incCol(string tcCol) | |
{ | |
if (tcCol == "") return "A"; | |
string fPart = tcCol.Substring(0, tcCol.Length - 1); | |
char lChar = tcCol[tcCol.Length - 1]; | |
if (lChar == 'Z') return incCol(fPart) + "A"; | |
return fPart + ++lChar; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment