C#中英文混合字符串截取函数_C#教程

分享到:

/// <summary>
        /// 截断字符串
        /// </summary>
        /// <param name="maxLength">最大长度</param>
        /// <param name="str">原字符串</param>
        /// <returns></returns>
        public static string CutStr(int maxLength, string str)
        {
            string temp = str;
            if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= maxLength)
            {
                return temp;
            }
            for (int i = temp.Length; i >= 0; i--)
            {
                temp = temp.Substring(0, i);
                if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= maxLength - 3)
                {
                    return temp + "...";
                }
            }
            return "...";
        }
昵    称:
验证码:

相关文档: