zoukankan      html  css  js  c++  java
  • 截取指定长度的字符串,区分汉字和字符

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

  • 相关阅读:
    【转】cocos2d-x使用第三方的TTF字体库
    CCControlSlider和CCControlStepper用法
    Cocos2d-x中获取设备语言的方法
    ccrendertexture
    【转】如何使用KeyChain保存和获取UDID
    【luogu P4777】【模板】扩展中国剩余定理(EXCRT)(数论)
    【luogu P1495】【模板】中国剩余定理(CRT)/曹冲养猪(数论)
    【luogu P3980】Volunteer / 志愿者招募(网络流)
    凡喵识图 / Image Recognition(鸽笼原理)(模拟)
    回文树(并查集)(倍增)(LCA)(ST 表)
  • 原文地址:https://www.cnblogs.com/weichuo/p/1378932.html
Copyright © 2011-2022 走看看