Skip to content

Instantly share code, notes, and snippets.

@sayurin
Last active November 29, 2020 06:40
Show Gist options
  • Save sayurin/497b6998c811c6e28daebca79ac866a4 to your computer and use it in GitHub Desktop.
Save sayurin/497b6998c811c6e28daebca79ac866a4 to your computer and use it in GitHub Desktop.
using System;
static void Test(string columnName) => Console.WriteLine("{0,-3} => {1,4} => {2,-3}", columnName, columnName.ToColumnNumber(), columnName.ToColumnNumber().ToColumnName());
Test("A");
Test("Z");
Test("AA");
Test("ZZ");
Test("AAA");
Test("BBB");
public static class StringEx {
static int ToNumber(char ch) =>
'A' <= ch && ch <= 'Z' ? ch - 'A' + 1 :
'a' <= ch && ch <= 'z' ? ch - 'a' + 1 :
throw new ArgumentOutOfRangeException("The string must contain only A to Z.");
public static int ToColumnNumber(this string @this) =>
@this.Length switch {
1 => ToNumber(@this[0]),
2 => ToNumber(@this[0]) * 26 + ToNumber(@this[1]),
3 => ToNumber(@this[0]) * 26 * 26 + ToNumber(@this[1]) * 26 + ToNumber(@this[2]),
_ => throw new ArgumentOutOfRangeException("The string length must be 1 to 3."),
};
}
public static class IntegerEx {
const string _1digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string _2digits = "AAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAUAVAWAXAYAZBABBBCBDBEBFBGBHBIBJBKBLBMBNBOBPBQBRBSBTBUBVBWBXBYBZCACBCCCDCECFCGCHCICJCKCLCMCNCOCPCQCRCSCTCUCVCWCXCYCZDADBDCDDDEDFDGDHDIDJDKDLDMDNDODPDQDRDSDTDUDVDWDXDYDZEAEBECEDEEEFEGEHEIEJEKELEMENEOEPEQERESETEUEVEWEXEYEZFAFBFCFDFEFFFGFHFIFJFKFLFMFNFOFPFQFRFSFTFUFVFWFXFYFZGAGBGCGDGEGFGGGHGIGJGKGLGMGNGOGPGQGRGSGTGUGVGWGXGYGZHAHBHCHDHEHFHGHHHIHJHKHLHMHNHOHPHQHRHSHTHUHVHWHXHYHZIAIBICIDIEIFIGIHIIIJIKILIMINIOIPIQIRISITIUIVIWIXIYIZJAJBJCJDJEJFJGJHJIJJJKJLJMJNJOJPJQJRJSJTJUJVJWJXJYJZKAKBKCKDKEKFKGKHKIKJKKKLKMKNKOKPKQKRKSKTKUKVKWKXKYKZLALBLCLDLELFLGLHLILJLKLLLMLNLOLPLQLRLSLTLULVLWLXLYLZMAMBMCMDMEMFMGMHMIMJMKMLMMMNMOMPMQMRMSMTMUMVMWMXMYMZNANBNCNDNENFNGNHNINJNKNLNMNNNONPNQNRNSNTNUNVNWNXNYNZOAOBOCODOEOFOGOHOIOJOKOLOMONOOOPOQOROSOTOUOVOWOXOYOZPAPBPCPDPEPFPGPHPIPJPKPLPMPNPOPPPQPRPSPTPUPVPWPXPYPZQAQBQCQDQEQFQGQHQIQJQKQLQMQNQOQPQQQRQSQTQUQVQWQXQYQZRARBRCRDRERFRGRHRIRJRKRLRMRNRORPRQRRRSRTRURVRWRXRYRZSASBSCSDSESFSGSHSISJSKSLSMSNSOSPSQSRSSSTSUSVSWSXSYSZTATBTCTDTETFTGTHTITJTKTLTMTNTOTPTQTRTSTTTUTVTWTXTYTZUAUBUCUDUEUFUGUHUIUJUKULUMUNUOUPUQURUSUTUUUVUWUXUYUZVAVBVCVDVEVFVGVHVIVJVKVLVMVNVOVPVQVRVSVTVUVVVWVXVYVZWAWBWCWDWEWFWGWHWIWJWKWLWMWNWOWPWQWRWSWTWUWVWWWXWYWZXAXBXCXDXEXFXGXHXIXJXKXLXMXNXOXPXQXRXSXTXUXVXWXXXYXZYAYBYCYDYEYFYGYHYIYJYKYLYMYNYOYPYQYRYSYTYUYVYWYXYYYZZAZBZCZDZEZFZGZHZIZJZKZLZMZNZOZPZQZRZSZTZUZVZWZXZYZZ";
public static string ToColumnName(this int @this) =>
@this <= 0 ? throw new ArgumentOutOfRangeException("Should be a value greater than 0.") :
@this <= 26 ? _1digits.Substring(@this - 1, 1) :
@this <= 27 * 26 ? _2digits.Substring((@this - 27) * 2, 2) :
string.Concat((char)('A' + @this / 26 / 26 - 1), (char)('A' + @this / 26 % 26 - 1), (char)('A' + @this % 26 - 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment