//字符是否为汉字
public bool IsChinese(char c)
{
return (int)c >= xE && (int)c <= xFA;
}
//获得字节长度
private int getLengthb(string str)
{
return SystemTextEncodingDefaultGetByteCount(str);
}
//c#的中英文混合字符串截取指定长度startidx从开始 by
public string getStrLenB(string str int startidx int len)
{
int Lengthb = getLengthb(str);
if (startidx + > Lengthb)
{
return ;
}
int j = ;
int l = ;
int strw = ;//字符的宽度
bool b = false;
string rstr = ;
for (int i = ; i < strLength; i++)
{
char c = str[i];
if (j >= startidx)
{
rstr = rstr + c;
b = true;
}
if (IsChinese(c))
{
strw = ;
}
else
{
strw = ;
}
j = j + strw;
if (b)
{
l = l + strw;
if ((l+)>= len) break;
}
}
return rstr;
}
private void button_Click(object sender EventArgs e) //测试by yl
{
MessageBoxShow(getStrLenB(gisoracle欢迎你gisoracle ));//gisoracle
MessageBoxShow(getStrLenB(gisoracle欢迎你gisoracle ));//isoracle欢
MessageBoxShow(getStrLenB(gisoracle欢迎你gisoracle ));//isoracle欢
}