Encoding encoding = Encoding.GetEncoding("gb2312"); 与byte[] ping = Encoding.UTF8.GetBytes(inputString);区别
2015-07-23 15:51
387 查看
Encoding encoding = Encoding.GetEncoding("gb2312"); 与byte[] ping = Encoding.UTF8.GetBytes(inputString);区别
public static string SubString(string inputString, int length)
{
byte[] ping = Encoding.UTF8.GetBytes(inputString);
int count=Encoding.UTF8.GetByteCount(inputString);
if (count <= length * 2)
{
return inputString;
}
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
}
tempString += inputString.Substring(i, 1);
if (tempLen >= length * 2)
break;
}
return tempString;
}
public static string GetSub(string sub, int length) { if (sub == null) return string.Empty; int len = length * 2; //aequilateLength为中英文等宽长度,cutLength为要截取的字符串长度 int aequilateLength = 0, cutLength = 0; Encoding encoding = Encoding.GetEncoding("gb2312");
string cutStr = sub.ToString(); int strLength = cutStr.Length; byte[] bytes; for (int i = 0; i < strLength; i++) { bytes = encoding.GetBytes(cutStr.Substring(i, 1)); if (bytes.Length == 2)//不是英文 aequilateLength += 2; else aequilateLength++;
if (aequilateLength <= len) cutLength += 1;
if (aequilateLength > len) return cutStr.Substring(0, cutLength);//+ "..." } return cutStr; }
public static string SubString(string inputString, int length)
{
byte[] ping = Encoding.UTF8.GetBytes(inputString);
int count=Encoding.UTF8.GetByteCount(inputString);
if (count <= length * 2)
{
return inputString;
}
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
string tempString = "";
byte[] s = ascii.GetBytes(inputString);
for (int i = 0; i < s.Length; i++)
{
if ((int)s[i] == 63)
{
tempLen += 2;
}
else
{
tempLen += 1;
}
tempString += inputString.Substring(i, 1);
if (tempLen >= length * 2)
break;
}
return tempString;
}
public static string GetSub(string sub, int length) { if (sub == null) return string.Empty; int len = length * 2; //aequilateLength为中英文等宽长度,cutLength为要截取的字符串长度 int aequilateLength = 0, cutLength = 0; Encoding encoding = Encoding.GetEncoding("gb2312");
string cutStr = sub.ToString(); int strLength = cutStr.Length; byte[] bytes; for (int i = 0; i < strLength; i++) { bytes = encoding.GetBytes(cutStr.Substring(i, 1)); if (bytes.Length == 2)//不是英文 aequilateLength += 2; else aequilateLength++;
if (aequilateLength <= len) cutLength += 1;
if (aequilateLength > len) return cutStr.Substring(0, cutLength);//+ "..." } return cutStr; }
相关文章推荐
- 【linux】gdb调试
- sersync+rsync原理及部署
- android TextView 显示字数的限制问题
- 学习javaWeb开发-Servlet
- IOS And WCF 上传文件
- 腾讯云移动推送信鸽后台探秘:海量终端 秒级送达
- HDU 1698 Just a Hook(线段树+lazy)
- 矩阵求逆
- 合并排序的递归实现
- iOS 自己封装的网络请求,json解析的类
- STM32启动文件2.02过程详解
- 再会鸟哥LINU
- 关于数据库迭代更新
- Word 中添加目录的一般方法
- iOS7+ 扫描二维码和条形码实现 耗时操作
- hdu3555 Bomb
- 一种简单有效的个性化推荐方法
- Java学习总结:反射机制
- JavaScript对象继承的方法
- CentOS的基础优化