您的位置:首页 > 编程语言 > C#

C# 汉字的字符串截取指定字节的长度

2013-12-09 22:44 471 查看
int index = 0;
int setCharCount = 74;
string str1 = "三星 SCH-I829 电信3G手机(优雅白)CDMA2000/GSM 双模双待双通";
byte[] bt = new ASCIIEncoding().GetBytes(str1);
if (bt.Length > setCharCount)//当超过需要的字节时
{
Array.Resize(ref bt, setCharCount);//获取需要长度的字节
for (int i = 0; i < bt.Length; i++)
if (bt[i] == 63)index++; //计算汉字数
str1.Substring(0, index + (setCharCount - index*2)); //截取所需长度的字符串
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: