This article is a mirror article of machine translation, please click here to jump to the original article.

View: 18567|Reply: 0

[ASP.NET] C# intercepts the length of the string string in 3 ways

[Copy link]
Posted on 5/22/2015 9:43:04 PM | | |
Method 1:
  1. /// <summary>
  2.         /// 截取指定长度的字符串
  3.         /// </summary>
  4.         /// <param name="str">要截取的字符串</param>
  5.         /// <param name="len">要截取的长度</param>
  6.         /// <param name="flag">截取后是否加省略号(true加,false不加)</param>
  7.         /// <returns></returns>
  8.         public static string CutString(string str, int len, bool flag)
  9.         {
  10.             string _outString = "";
  11.             int _len = 0;
  12.             for (int i = 0; i < str.Length; i++)
  13.             {
  14.                 if (Char.ConvertToUtf32(str, i) >= Convert.ToInt32("4e00", 16) && Char.ConvertToUtf32(str, i) <= Convert.ToInt32("9fff", 16))
  15.                 {
  16.                     _len += 2;
  17.                     if (_len > len)//截取的长度若是最后一个占两个字节,则不截取
  18.                     {
  19.                         break;
  20.                     }
  21.                 }
  22.                 else
  23.                 {
  24.                     _len++;
  25.                 }


  26.                 try
  27.                 {
  28.                     _outString += str.Substring(i, 1);
  29.                 }
  30.                 catch
  31.                 {
  32.                     break;
  33.                 }
  34.                 if (_len >= len)
  35.                 {
  36.                     break;
  37.                 }
  38.             }
  39.             if (str != _outString && flag == true)//判断是否添加省略号
  40.             {
  41.                 _outString += "...";
  42.             }
  43.             return _outString;
  44.         }
Copy code
Method 2:
  1. /// <summary>
  2.         /// 截取指定长度的字符串
  3.         /// </summary>
  4.         /// <param name="str">要截取的字符串</param>
  5.         /// <param name="len">要截取的长度</param>
  6.         /// <param name="flag">截取后是否加省略号(true加,false不加)</param>
  7.         /// <returns></returns>
  8.         public static string CutString(string str, int len, bool flag)
  9.         {
  10.             System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
  11.             byte[] bts = ascii.GetBytes(str);
  12.             int _len = 0;
  13.             string _outString = "";
  14.             for (int i = 0; i < bts.Length; i++)
  15.             {
  16.                 if ((int)bts[i] == 63)//两个字符
  17.                 {
  18.                     len += 2;
  19.                     if (_len > len)//截取的长度若是最后一个占两个字节,则不截取
  20.                     {
  21.                         break;
  22.                     }
  23.                 }
  24.                 else
  25.                 {
  26.                     len += 1;
  27.                 }

  28.                 try
  29.                 {
  30.                     _outString += str.Substring(i, 1);
  31.                 }
  32.                 catch
  33.                 {
  34.                     break;
  35.                 }

  36.                 if (_len >= len)
  37.                 {
  38.                     break;
  39.                 }
  40.             }
  41.             if (str != _outString && flag == true)//判断是否添加省略号
  42.             {
  43.                 _outString += "...";
  44.             }
  45.             return _outString;
  46.         }
Copy code
Method 3:
  1. /// <summary>
  2.         /// 截取指定长度的字符串
  3.         /// </summary>
  4.         /// <param name="str">要截取的字符串</param>
  5.         /// <param name="len">要截取的长度</param>
  6.         /// <param name="flag">截取后是否加省略号(true加,false不加)</param>
  7.         /// <returns></returns>
  8.         public static string CutString(string str, int len, bool flag)
  9.         {
  10.             if (str == null || str.Length == 0 || len <= 0)
  11.             {
  12.                 return string.Empty;
  13.             }

  14.             int l = str.Length;

  15.             #region 计算长度
  16.             int clen = 0;
  17.             while (clen < len && clen < l)
  18.             {
  19.                 //每遇到一个中文,则将目标长度减一。
  20.                 if ((int)str[clen] > 128)
  21.                 {
  22.                     len--;
  23.                 }
  24.                 clen++;
  25.             }
  26.             #endregion

  27.             if (clen < l)
  28.             {
  29.                 return flag ? str.Substring(0, clen) + "..." : str.Substring(0, clen);
  30.             }
  31.             else
  32.             {
  33.                 return str;
  34.             }
  35.         }
Copy code






Previous:asp.net the standard three-tier framework used for the exam
Next:In the GridView control, the text content displays the full html source code
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com