in c#, how can i build up array from A to ZZ that is similar to the way that excel orders columns

后端 未结 10 961
情深已故
情深已故 2020-12-18 00:48

i am looking for code that can generate an array where the first item is A, then B, then C . . .after Z i

相关标签:
10条回答
  • 2020-12-18 01:16

    To complement Vlad's answer, here's the reverse operation of ToBase26(int):

    static long ToBase10(string str)
    {
       if (string.IsNullOrWhiteSpace(str)) return 0;
       var value = str[0] - 'A' + 1;
    
       return (long) (value * Math.Pow(26, str.Length - 1) + 
              ToBase10(str.Substring(1, str.Length - 1)));
    }
    
    0 讨论(0)
  • 2020-12-18 01:17

    Here's one way. :)

    string[] values =
      Enumerable.Range(0, 27 * 26)
      .Select(
        n => new String(
          new[] { (char)('@' + n / 26), (char)('A' + n % 26) },
          n < 26 ? 1 : 0, n < 26 ? 1 : 2
        )
      )
      .ToArray();
    
    0 讨论(0)
  • 2020-12-18 01:17
    class Program
    {
        public static string IntegerToExcelColumn(int col)
        {
            // I've put a 256 upper bound here because Excel 2003 
            // allows only 256 columns. Change it if you're using 
            // Excel 2007 or 2010.
            Debug.Assert(col >= 1 && col <= 256);
    
            if (col >= 1 && col <= 26)
            {
                return ((char)(((int)'A') + (col - 1))).ToString();
            }
    
            // I've put a 256 upper bound here because Excel 2003 
            // allows only 256 columns. Change it if you're using 
            // Excel 2007 or 2010.
            if (col > 26 && col <= 256)
            {
                int rem = col % 26;
                int pri = col / 26;
                if (rem == 0)
                {
                    rem = 26;
                    pri--;
                }
                char[] buffer = new char[2];
                buffer[0] = (char)(((int)'A') + (pri - 1));
                buffer[1] = (char)(((int)'A') + (rem - 1));
                return new string(buffer);
            }
    
            return "";
        }
    
        static void Main(string[] args)
        {
            string[] columns= new string[255];
            for (int i = 1; i <= 255; i++)
                columns[i-1] = IntegerToExcelColumn(i);
            foreach(var col in columns)
                Console.WriteLine(col);
        }
    }
    
    0 讨论(0)
  • 2020-12-18 01:20

    Have a look at an ASCII table and take note of the values of the characters. You should be able to work out a loop to increment the character from A to Z for as many times as you need to replicate the characters. :)

    0 讨论(0)
  • 2020-12-18 01:25

    One of the ways is:

    IEnumerable<string> generate()
    {
        for (char c = 'A'; c <= 'Z'; c++)
            yield return new string(c, 1);
        for (char c = 'A'; c <= 'Z'; c++)
            for (char d = 'A'; d <= 'Z'; d++)
                yield return new string(new[] { c, d });
    }
    

    Edit:
    you can actually produce "infinite" sequence (bounded by maximal long value) with somewhat more complicated code:

    string toBase26(long i)
    {
        if (i == 0) return ""; i--;
        return toBase26(i / 26) + (char)('A' + i % 26);
    }
    
    IEnumerable<string> generate()
    {
        long n = 0;
        while (true) yield return toBase26(++n);
    }
    

    This one goes like that: A, B, ..., Z, AA, AB, ..., ZZ, AAA, AAB, ... etc:

    foreach (var s in generate().Take(200)) Console.WriteLine(s);
    
    0 讨论(0)
  • 2020-12-18 01:29

    You could generate numbers using Enumerable.Range and cast them to char to generate A-Z. Next step is to combine them.

    0 讨论(0)
提交回复
热议问题