您当前位置:编程帮手 > 知识 > 知识 > C# > String > 内容
代码库
2017-01-26 18:17:51 访问(1335) 赞(0) 踩(0)
/// <summary> /// 获得字符串长度[中文长度] /// </summary> /// <param name="str"> /// <returns></returns> public int StrGetLength(string str) { if (str == null || str.Length == 0) return 0; ASCIIEncoding ascii = new ASCIIEncoding(); int tempLen = 0; byte[] s = ascii.GetBytes(str); int iLen = s.Length; for (int i = 0; i < iLen; i++) { if ((int)s[i] == 63) { tempLen += 2; } else { tempLen += 1; } } return tempLen; }
上一条:
下一条: