截取字符串:C# 截取字符串来源: 发布时间:星期二, 2008年12月2日 浏览:200次 评论:0
#region 截取字符串
/// <summary> /// 截取字符串 /// </summary> /// <param name="len">截取长度</param> /// <param name="symbol">需截取字符串</param> /// <returns></returns> public static String GetLimitLengthString(int length, String str) { str = NoHTML(str); string temp = str; int j = 0; int k = 0; for (int i = 0; i < temp.Length; i++) { if (Regex.IsMatch(temp.Substring(i, 1), @"[\u4e00-\u9fa5]+")) { j += 2; } else { j += 1; } if (j <= length) { k += 1; } if (j >= length) { return temp.Substring(0, k) + "..."; } } return temp; } #endregion #region 过滤掉HTML标签 /// <summary> /// 过滤掉HTML标签 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string NoHTML(string str) { str = Regex.Replace(str, @"(\<.[^\<]*\>)", " ", RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase); str = Regex.Replace(str, @"(\<\/[^\<]*\>)", " ", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); str = Regex.Replace(str, " ", ""); return str; } #endregion 0
相关文章
读者评论
发表评论 |