zoukankan      html  css  js  c++  java
  • string截断

    public static string GetFirstString(string stringToSub, int length) 
            {
                Regex regex = new Regex("[u4e00-u9fa5]+", RegexOptions.Compiled);
                char[] stringChar = stringToSub.ToCharArray();
                StringBuilder sb = new StringBuilder();
                int nLength = 0;
                bool isCut=false;
                for(int i = 0; i < stringChar.Length; i++) 
                {
                    if (regex.IsMatch((stringChar[i]).ToString())) 
                    {
                        sb.Append(stringChar[i]);
                        nLength += 2;
                    }
                    else 
                    {
                        sb.Append(stringChar[i]);
                        nLength = nLength + 1;
                    }

                    if (nLength > length)
                    {
                        isCut=true;
                        break;
                    }
                }
                if(isCut)
                    return sb.ToString()+"..";
                else
                    return sb.ToString();
            }
  • 相关阅读:
    windows下命令行
    利用border画三角形
    正则
    flex布局
    css笔记
    W3C标准
    SEO相关
    左边固定,右边自适应(解决方案)
    容错性测试的测试点
    Charles安装及使用教程
  • 原文地址:https://www.cnblogs.com/baozhu/p/4292922.html
Copyright © 2011-2022 走看看