字符串加1

来源:互联网 发布:pplive网络电视直播 编辑:程序博客网 时间:2024/05/08 00:34

        string strCode = "001001999";
        string strChar = string.Empty;
        string strRight, strMiddle, strLeft;
        strRight = string.Empty;
        strMiddle = string.Empty;
        strLeft = string.Empty;


        int j = 0;

        strChar = string.IsNullOrEmpty(strCode) ? "001" : strCode;

        if (strCode.Substring(strChar.Length - 1).Equals("9"))
        {
            for(int i=0; i < strCode.Length; i++)
            {
                if ( strCode.Substring((strCode.Length - i - 1),1).Equals("9"))
                    j++;
                else
                    break;

            }


            strRight = strRight.PadRight(j, '0');   //有多少个9补多少个零
            strMiddle = Convert.ToString(int.Parse(strCode.Substring(strCode.Length - j - 1, 1))+ 1);   //加1后的值
            strLeft = strCode.Substring(0, strCode.Length - j - 1); //
            strChar = strLeft + strMiddle + strRight;

        }
        else
        {
            strRight = Convert.ToString(int.Parse(strCode.Substring(strCode.Length - 1)) + 1);
            strLeft = strCode.Substring(0,strCode.Length - 1);
            strChar = strLeft + strRight;
        }


        Response.Write(strChar);

原创粉丝点击