Utils-统计中英文混合的字符串的字数

        //调用方法
        protected virtual bool IsFieldValid(string toValidString,int maxLength,int minLength)
        {
            long len = LenOfStr(toValidString);
            if(len>maxLength||len<minLength)
                return false;
            return true;
        }

        /// 写在工具类中

        /// 统计中英文混合的字符串的字数

        /// <param name="str">输入的字符串</param>

        /// <returns>返回字符串长度</returns>

        public static long LenOfStr(string str)

        {              
            if (str.Length == 0)
                return 0;

            long wCnt;

            wCnt = 0;

            char[] ch;

            ch = str.ToCharArray();

            uint kk;

            for(long iCnt = 0; iCnt <ch.Length;iCnt++ )
            {        

                kk= (uint)ch[iCnt];

                if(IsSep(ch[iCnt]))

                    continue;

                if((uint)ch[iCnt] > 127 )
                {

                    wCnt++;

                    continue;
                }

                if(iCnt == 0)
                {

                    wCnt++;

                    continue;
                }

                if(IsSep(ch[iCnt - 1]) || (uint)ch[iCnt - 1] > 127 )
                {

                    wCnt++;

                    continue;
                }

            }    

            return wCnt;
        }

 

        public static bool IsSep(char ch)
        {

            return Char.IsSeparator(ch);     
        }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容