Encoding Encoding = encoding.getencoding ("gb2312"); With 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) { & nbsp; if (sub = = null) return string. Empty; int len = length * 2; //aequilatelength for the Chinese and English width length, Cutlength is the length of the string to intercept 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++) & nbsp; { bytes = Encoding. GetBytes (Cutstr.substring (i, 1)); if (bytes. Length = = 2)//not English Aequilatelength + = 2; Else aequilatelength++;
if (aequilatelength <= len) cutlength + = 1;
if (Aequilatelength > Len) return cutstr.substring (0, cutlength);//+ "..."} return cutstr; }
Encoding Encoding = encoding.getencoding ("gb2312"); With byte[] ping = Encoding.UTF8.GetBytes (inputstring);