Sun_china

交流更多,收获更多?

博客园 首页 新随笔 联系 订阅 管理

  /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="value"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string SubString(string stringToSub, int length)
        {
            Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
            char[] stringChar = stringToSub.ToCharArray();
            StringBuilder Builder = new StringBuilder();
            int nLength = 0;

            for (int i = 0; i < stringChar.Length; i++)
            {
                if (regex.IsMatch((stringChar[i]).ToString()))
                {
                    Builder.Append(stringChar[i]);
                    nLength += 2;
                }
                else
                {
                    Builder.Append(stringChar[i]);
                    nLength = nLength + 1;
                }

                if (nLength > length)
                    break;
            }

            return Builder.ToString();
        }

posted on 2008-04-02 14:35  Sun_china  阅读(254)  评论(0编辑  收藏  举报