Length of the truncated string (in Chinese and English)

Source: Internet
Author: User

}} return System.Text.Encoding.Unicode.GetString (list. ToArray ()); varchar--20 bytes will have >=10 characters public static string cutstring (string value, int length, bool ellipsis, Cuttype cuttype) {Valu E = value. Trim (); if (value. Length = = 0) return string. Empty; if (Cuttype = = Cuttype.nvarchar) {if (value. Length > Length/2) {value = value. Substring (0, LENGTH/2); if (ellipsis) return value + "..."; }} else {string resultstring = string. Empty; byte[] MyByte = System.Text.Encoding.GetEncoding ("GBK"). GetBytes (value); if (Mybyte.length > Length) {resultstring = encoding.getencoding ("GBK"). GetString (mybyte, 0, length); String Lastchar = Resultstring.substring (resultstring.length-1, 1); if (lastchar.equals (value). Substring (resultstring.length-1, 1))) {value = resultstring;} If the last character of the intercept is equal to the character in the same position in the original input string, then the intercept completes else//if not equal, subtract one byte and then intercept {value = encoding.getencoding ("GBK"). GetString (mybyte, 0, length-1); } if (ellipsis) return value + "..."; return value; }} return value; } #region The function of the truncated string, partitioning the functions of English////////////////////////////or processed strings public static string left (string mText, int Byteco UNT) {if (ByteCount < 1) return mText; if (System.Text.Encoding.Default.GetByteCount (mText) <= byteCount) {return MText; } else {byte[] txtbytes = System.Text.Encoding.Default.GetBytes (MText); byte[] newbytes = new byte[bytecount-4]; for (i NT i = 0; i < byteCount-4; i++) {Newbytes[i] = Txtbytes[i];} string OutPut = System.Text.Encoding.Default.GetString (newbytes) + "..."; if (Output.endswith ("?...") = = True) {OutPut = output.substring (0, output.length-4); OutPut + = "...";} return OutPut; }} #endregion}/////////Intercept word Fu Yi, varchar--English one byte, Chinese two bytes, nvarchar--whether both Chinese and English are two bytes//public enum Cuttype {Varchar, NVarchar }

Length of the truncated string (Chinese and English)

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.