/// <summary> /// 截取字符串,取固定字节数,多余的用...替代,为空或者null的返回空字符串 /// </summary> /// <param name="s">要截取的字符串</param> /// <param name="len">要截取的字节数</param> /// <param name="emptyReturn">当字符串为null或者空时,返回的字符串,比如"暂未内容"之类的</param> /// <returns>截取后的字符串</returns> public static string CutString(string s, int len,string emptyReturn) { if (string.IsNullOrEmpty(s)) return emptyReturn; if (System.Text.Encoding.Default.GetByteCount(s) <= len)//如果长度比需要的长度n小,返回原字符串 { return s; } int t = 0 ,i = 0 ,tlen = s.Length; char[] cs = s.ToCharArray(); for (i = 0; i < tlen && t < len; i++) { if ((int)cs[i] >= 0x4e00 && (int)cs[i] <= 0x9fa5)//是否汉字 { t += 2; } else { t++; } } return s.Substring(0, i) + "..."; }