C# 中英文字符串等长截取

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/My_ben/article/details/83931618

中英文字符串等长截取,代码如下:

   public static string CutString(string str, int len)
   {
       if (String.IsNullOrEmpty(str))
       {
           return string.Empty;
       }
       int strlen = str.Length;
       #region 计算长度
       int cutlen = 0;
       while (cutlen < len && cutlen < strlen)
       {
          if ((int)str[cutlen] > 128)
          {
               len--;
          }
          cutlen++;
        }
        #endregion
        if (cutlen < strlen)
        {
           return str.Substring(0, cutlen) + " ...";
        }
        else
        {
            return str;
        }
   }

猜你喜欢

转载自blog.csdn.net/My_ben/article/details/83931618