//调用方法
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);
}
网友评论