本文实例讲述了C#实现中英文混合字符串截取的要领,,是C#字符串操纵中非每每用的一个要领。分享给大家供大家参考之用。具体要领如下:
具体成果代码如下:
/// <summary> /// 截取中英文混合字符串 /// </summary> /// <param>字符文本</param> /// <param>截取长度</param> /// <param>replacetxt替换的多余字符</param> /// <returns></returns> public string GetString(string text, int length, string replacetxt) { int strLength = 0; StringBuilder strb = new StringBuilder(); char[] Temp = text.ToCharArray(); for (int i = 0; i != Temp.Length; i++) { if (strLength >= length) // { strb.Append(replacetxt); break; } else { if (((int)Temp[i]) < 255) //大于255的都是汉字或者特殊字符 { strLength++; } else { strLength = strLength + 2; } strb.Append(Temp[i]); } } return strb.ToString(); }