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();
            }
  • 相关阅读:
    [BJOI2006]狼抓兔子
    [HNOI2016]最小公倍数
    hihocoder 1419 重复旋律4
    [NOI2015]品酒大会
    [SDOI2016]生成魔咒
    [ZJOI2009]狼和羊的故事
    BZOJ4361 isn
    [SDOI2009]虔诚的墓主人
    BZOJ 3329 Xorequ
    [ZJOI2013]丽洁体
  • 原文地址:https://www.cnblogs.com/baozhu/p/4292922.html
Copyright © 2011-2022 走看看