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();
            }
  • 相关阅读:
    Web开发需要掌握的
    使用this关键字,构造函数的相互调用
    FCKEditor.Net在Asp.Net MVC中的配置
    技术收集
    System.Collections命名空间
    C#中的托管堆,托管
    C#中的数据类型
    sql server部分主要代码
    Visual C#常用函数和方法集汇总
    C#委托和事件
  • 原文地址:https://www.cnblogs.com/baozhu/p/4292922.html
Copyright © 2011-2022 走看看