/// <summary>
/// 截取指定长度(宽度一样)字符串,区分中英文 /// </summary> /// <param name="str">要截取的字符串</param> /// <param name="length">截取长度</param> /// <returns>截取后的字符串</returns> public static string CutStr(string str, int length) { if (length < 1) return str; if (System.Text.Encoding.Default.GetByteCount(str) <= length) { return str; } else { byte[] txtBytes = System.Text.Encoding.Default.GetBytes(str); byte[] newBytes = new byte[length - 4]; for (int i = 0; i < length - 4; i++) { newBytes[i] = txtBytes[i]; } return System.Text.Encoding.Default.GetString(newBytes) + "... "; } } |