• 截取指定长度的字符串,区分汉字和字符


    using System.Text;
    using System.Text.RegularExpressions;
         

     

               int current = 30;
                int location_http = lblContext.Text.IndexOf("http");
                int location_img = lblContext.Text.IndexOf("img");
                if (location_http < current && location_http > 0)
                {
                    current = location_http;
                }
                if (location_img < current && location_img > 0)
                {
                    current = location_img;
                }
     

                lblContext.Text = GetFirstString(lblContext.Text, current);
         

       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();
            }

  • 相关阅读:
    Sencha的Eclipse插件提示和技巧
    《敏捷软件开发过程及最佳实践》培训总结
    《Sencha应用程序的UI测试 》一文的示例分析
    Ext JS 4.2 Beta版发布
    迅速解决resin或者tomcat启动闪一下就消失的问题
    import javax.servlet 出错
    有爱好者把我的CMS管理系统改成了JAVA版,有兴趣的可以看看
    一个@符号引发的血案:Access数据库无法更新
    Windows 7下如何安装和配置IIS 7和ASP
    .Net中Freetextbox_1.6.3的使用与ftb.imagegallery.aspx安全修正
  • 原文地址:https://www.cnblogs.com/weichuo/p/1378932.html
Copyright © 2020-2023  润新知