标签:
private int GetStrByteLength(string str)
{
return System.Text.Encoding.Default.GetByteCount(str);
}
private string SubStrLenth(string str, int startIndex, int length)
{
int strlen = GetStrByteLength(str);
if (startIndex + 1 > strlen)
{
return "";
}
int j = 0;//记录遍历的字节数
int L = 0;//记录每次截取开始,遍历到开始的字节位,才开始记字节数
int strW = 0;//字符宽度
bool b = false;//当每次截取时,遍历到开始截取的位置才为true
string restr = string.Empty;
for (int i = 0; i < str.Length; i++)
{
char C = str[i];
if (ISChinese(C))
{
strW = 2;
}
else
{
strW = 1;
}
if ((L == length-1) && (L + strW > length))
{
b = false;
break;
}
if (j >= startIndex)
{
restr += C;
b = true;
}
j += strW;
if (b)
{
L += strW;
if (((L + 1) > length))
{
b = false;
break;
}
}
}
return restr;
}
标签:
原文地址:http://www.cnblogs.com/DTWolf/p/4670149.html