// Whether the character is a Chinese character
Public bool ischinese (char C)
{
Return (INT) C >=0x4e00 & (INT) C <= 0x9fa5;
}
// Get the byte length
Private int getlengthb (string Str)
{
Return System. Text. encoding. Default. getbytecount (STR );
}
// C # specifies the length of the string, startidx from 0 by gisoracle@126.com
Public String getstrlenb (string STR, int startidx, int Len)
{
Int lengthb = getlengthb (STR );
If (startidx + 1> lengthb)
{
Return "";
}
Int J = 0;
Int L = 0;
Int strw = 0; // character width
Bool B = false;
String rstr = "";
For (INT I = 0; I <Str. length; I ++)
{
Char c = STR [I];
If (j> = startidx)
{
Rstr = rstr + C;
B = true;
}
If (ischinese (c ))
{
Strw = 2;
}
Else
{
Strw = 1;
}
J = J + strw;
If (B)
{
L = L + strw;
If (L + 1)> = Len) break;
}
}
Return rstr;
}
Private void button4_click (Object sender, eventargs e) // test by yl gisoracle@126.com
{
MessageBox. Show (getstrlenb ("gisoracle welcomes you gisoracle", 0, 10); // gisoracle
MessageBox. Show (getstrlenb ("gisoracle welcome gisoracle", 1, 10); // isoracle Huan
MessageBox. Show (getstrlenb ("gisoracle welcomes you gisoracle", 2, 10); // isoracle Huan
}