标签:style blog color for ar div log ef
1 Encoding.Default.GetByteCount(checkString); =2 全角 =1 半角
/// <summary> /// 获取字符串的字节长度 /// </summary> /// <param name="str"></param> /// <returns></returns> public static int GetStringByteLength(this string str) { int j = 0; foreach (char c in str) { string checkString = c + ""; j += Encoding.Default.GetByteCount(checkString); } return j; }
/// <summary> /// 根据字节数截取字符串 /// </summary> /// <param name="s">输入的字符串</param> /// <param name="length">要截取的字节长度</param> /// <returns></returns> public static string bSubstring(this string s, int length) { if (length>=s.GetStringByteLength()) { return s; } string outStr=""; int j = 0; foreach (char c in s) { string checkString = c + ""; j += Encoding.Default.GetByteCount(checkString); outStr += c; if (j>=length) break; } return outStr; }
c# 判断字符是否是全角, 获取字符串的字节数 , 获取字符串指定长度字节数的字符串,布布扣,bubuko.com
c# 判断字符是否是全角, 获取字符串的字节数 , 获取字符串指定长度字节数的字符串
标签:style blog color for ar div log ef
原文地址:http://www.cnblogs.com/simadi/p/3918039.html