Code-Validator:验证字符串字节数长度范围

Posted storebook

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Code-Validator:验证字符串字节数长度范围相关的知识,希望对你有一定的参考价值。

ylbtech-Code-Validator:验证字符串字节数长度范围

 

1.返回顶部
1、
using System;
using System.Text;

namespace Sp.Common
{
    public class ValidatorHelper
    {
        /// <summary>  
        /// 验证字符串字节数长度范围  
        /// [若要验证固定长度,可传入相同的两个长度数值;每个汉字为两个字节长度]  
        /// </summary>  
        /// <param name="input">待验证的字符串</param>  
        /// <param name="lengthBegin">长度范围起始值(含)</param>  
        /// <param name="lengthEnd">长度范围结束值(含)</param>  
        /// <returns></returns>  
        public static bool IsStringByteLength(string input, int lengthBegin, int lengthEnd)
        {
            //int byteLength = Regex.Replace(input, @"[^x00-xff]", "ok").Length;  
            //if (byteLength >= lengthBegin && byteLength <= lengthEnd)  
            //{  
            //    return true;  
            //}  
            //return false;  
            int byteLength = Encoding.Default.GetByteCount(input);
            if (byteLength >= lengthBegin && byteLength <= lengthEnd)
                return true;
            else
                return false;
        }

    }
}
2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
 
 
6.返回顶部
 
技术图片 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

以上是关于Code-Validator:验证字符串字节数长度范围的主要内容,如果未能解决你的问题,请参考以下文章

Code-Validator:验证日期

Code-Validator:验证邮政编码

Code-Validator:验证电子邮箱

Code-Validator:验证手机号码

Code-Validator:验证身份证号

字节数,字符串长度,字符个数的区别?