using System; using System.Collections.Generic; using System.Text; using System.Globalization; using System.Text.RegularExpressions; namespace Framework { /// <summary> /// 字符串辅助类 /// </summary> public static class StringHelper { /// <summary> /// 截取字符串的后部分 /// </summary> /// <param name="source">原字符串</param> /// <param name="value">拆分字符串</param> /// <returns>截取后的字符串</returns> public static string SubstringAfter(this string source, string value) { if (string.IsNullOrEmpty(value)) { return source; } CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo; int index = compareInfo.IndexOf(source, value, CompareOptions.Ordinal); if (index < 0) { return string.Empty; } return source.Substring(index + value.Length); } /// <summary> /// 截取字符串的前部分 /// </summary> /// <param name="source">原字符串</param> /// <param name="value">拆分字符串</param> /// <returns>截取后的字符串</returns> public static string SubstringBefore(this string source, string value) { if (string.IsNullOrEmpty(value)) { return value; } CompareInfo compareInfo = CultureInfo.InvariantCulture.CompareInfo; int index = compareInfo.IndexOf(source, value, CompareOptions.Ordinal); if (index < 0) { return string.Empty; } return source.Substring(0, index); } /// <summary> /// 追加字符串,用分隔符分隔,默认分隔符为“,” /// </summary> /// <param name="sb">StringBulider对象</param> /// <param name="append">要追加的字符串</param> /// <param name="split">分隔符</param> public static void AppendString(this StringBuilder sb, string append, string split = ",") { if (sb.Length == 0) { sb.Append(append); return; } sb.Append(split); sb.Append(append); } /// <summary> /// 替换所有HTML标签为空 /// </summary> /// <param name="input">The string whose values should be replaced.</param> /// <returns>A string.</returns> public static string RemoveHtml(this string input) { var stripTags = new Regex("</?[a-z][^<>]*>", RegexOptions.IgnoreCase); return stripTags.Replace(input, string.Empty); } } }