中英文字符长度验验(转ASCII编码)

/// <summary>
        /// 字符长度检验
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public int StrLength(string str)
        {
            if (str.Equals(string.Empty))
                return 0;
            int strlen = 0;
            ASCIIEncoding strData = new ASCIIEncoding();
            //将字符串转换为ASCII编码的字节数字
            byte[] strBytes = strData.GetBytes(str);
            for (int i = 0; i <= strBytes.Length - 1; i++)
            {
                if (strBytes[i] == 63)  //中文都将编码为ASCII编码63,即"?"号
                    strlen++;
                strlen++;
            }
            return strlen;
        }

 

posted on 2013-05-12 20:02  小小池  阅读(246)  评论(0编辑  收藏  举报