/// <summary> /// 截取字段长度 /// </summary> /// <param name="str">需要截取的字符串</param> /// <param name="Len">最大长度限制</param> /// <returns></returns> public string CutStringByte(string str, int Len) { if (string.IsNullOrEmpty(str)) { return ""; } if (System.Text.Encoding.Default.GetByteCount(str) <= Len) { return str; } int i = 0;//字节数 int j = 0;//实际截取长度 foreach (char newChar in str) { if ((int)newChar > 127) { //汉字占两个字符 i += 2; } else { i++; } if (i <= Len) j++; else break; } str = str.Substring(0, j); return str; }