又一个字符串,由全角字符,半角字符构成,如何截取该字符串,全角算两个字符
又一个字符串,由全角字符,半角字符构成,如何截取该字符串,全角算两个字符 --------------------编程问答-------------------- 看看这个是你要的吗?/// <summary>
/// 取中文字串
/// </summary>
/// <param name="content">内容</param>
/// <param name="length">长度</param>
/// <returns></returns>
private string getLeftSubString(string content, int length)
{
Encoding encoding = Encoding.GetEncoding("gb2312");
StringBuilder sb = new StringBuilder();
int totalLength = 0;
foreach (char contentChar in content)
{
int size = encoding.GetByteCount(new char[] { contentChar });
if (totalLength + size > length)// -2
{
//sb.Append("..");
break;
}
sb.Append(contentChar);
totalLength += size;
}
content = content.Remove(0, sb.Length);
return sb.ToString();
} --------------------编程问答-------------------- //假设截取70个
Encoding d = Encoding.Default;
byte[] bs = d.GetBytes(str);
strOut = d.GetString(bs, 0, 70);
补充:.NET技术 , ASP.NET