Encoding Encoding = encoding.getencoding ("gb2312"); With byte[] ping = Encoding.UTF8.GetBytes (inputstring);

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.