您的位置:首页 > 其它

让字母自增,就像A-Z,Z后面就是AA-AZ,然后BA-BZ...

2017-07-14 09:54 197 查看
       string GetNextChars(string letter)

        {

            string letterTemp = letter.Trim();

            int length = letterTemp.Length;

            int res = 0;

            for (int i = 0; i < length; i++)//先转成数字 A=1 Z=26 AZ=52

            {

                res = res * 26 + letterTemp[i] - 'A' + 1;

            }

            res++;

            string endCol = string.Empty;

            string endColSignal = string.Empty;

            int iCnt = (res / 26);

            if (res >= 26 && res % 26 == 0)

            {

                int icell = iCnt - 2;

                endCol = (icell < 0 ? string.Empty : ((char)('A' + icell)).ToString()) + "Z";

            }

            else

            {

                endColSignal = (iCnt == 0 ? "" : ((char)('A' + (iCnt - 1))).ToString());

                int icell = res - iCnt * 26 - 1;

                if (icell < 0)

                    icell = 0;

                endCol = endColSignal + ((char)('A' + icell)).ToString();

            }

            return endCol;

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  A-Z
相关文章推荐