有时候一些需求,移除字符串前后指定的字符串。其实,如果没有仅移除一次的需求,使用系统自带的方法即可完成,使用Trim
、TrimEnd
、TrimStart
方法也比较快捷。当有一些特殊需求的时候,这些方法就不再方便。所以,就使用正则表达式
简单的实现相关需求。
字符串扩展代码实现
public static partial class Extensions
{
/// <summary>
/// 移除字符串前后指定的字符串
/// </summary>
/// <param name="value">字符串本身</param>
/// <param name="trimContent">将要移除字符前后的指定内容</param>
/// <param name="isRepeat">是否允许重复匹配</param>
/// <returns></returns>
public static string Trim(this string value, string trimContent = "",bool isRepeat=true)
{
if (string.IsNullOrWhiteSpace(value))
{
return "";
}
if (string.IsNullOrWhiteSpace(trimContent))
{
return value.Trim();
}
trimContent = Regex.Replace(trimContent, @"([.$\\^*+?\[\]()])", @"\$1");
string limit = isRepeat ? "+" : "";
string regexString = $"^(?:{trimContent}){limit}|(?:{trimContent}){limit}$";
value = Regex.Replace(value, regexString, "");
return value;
}
/// <summary>
/// 移除字符串开始指定的字符串
/// </summary>
/// <param name="value">字符串本身</param>
/// <param name="trimContent">将要在字符串开始移除的字符</param>
/// <param name="isRepeat">是否允许重复匹配</param>
/// <returns></returns>
public static string TrimStart(this string value, string trimContent = "", bool isRepeat = true)
{
if (string.IsNullOrWhiteSpace(value))
{
return "";
}
if (string.IsNullOrWhiteSpace(trimContent))
{
return value.TrimStart();
}
trimContent = Regex.Replace(trimContent, @"([.$\\^*+?\[\]()])", @"\$1");
string limit = isRepeat ? "+" : "";
string regexString = $"^(?:{trimContent}){limit}";
value = Regex.Replace(value, regexString, "");
return value;
}
/// <summary>
/// 移除字符串结尾指定的字符
/// </summary>
/// <param name="value">字符串本身</param>
/// <param name="trimContent">将要在字符串结尾移除的字符</param>
/// <param name="isRepeat">是否允许重复匹配</param>
/// <returns></returns>
public static string TrimEnd(this string value, string trimContent = "", bool isRepeat = true)
{
if (string.IsNullOrWhiteSpace(value))
{
return "";
}
if (string.IsNullOrWhiteSpace(trimContent))
{
return value.Trim();
}
trimContent = Regex.Replace(trimContent, @"([.$\\^*+?\[\]()])", @"\$1");
string limit = isRepeat ? "+" : "";
string regexString = $"(?:{trimContent}){limit}$";
value = Regex.Replace(value, regexString, "");
return value;
}
/// <summary>
///通过指定正则表达式替换指定内容
/// </summary>
/// <param name="value">字符串本身</param>
/// <param name="pattern">正则表达式</param>
/// <param name="replacement">替换内容</param>
/// <returns></returns>
public static string Replace(this string value, string pattern, string replacement = "")
{
if (string.IsNullOrWhiteSpace(value))
{
return "";
}
if (string.IsNullOrWhiteSpace(pattern))
{
throw new ArgumentException("pattern 不能为空");
}
value = Regex.Replace(value, pattern, replacement);
return value;
}
/// <summary>
/// 通过指定正则表达式替换指定内容
/// </summary>
/// <param name="value">字符串本身</param>
/// <param name="pattern">正则表达式</param>
/// <param name="replacement">替换内容</param>
/// <param name="options">正则表达式选项</param>
/// <returns></returns>
public static string Replace(this string value, string pattern, string replacement, RegexOptions options)
{
if (string.IsNullOrWhiteSpace(value))
{
return "";
}
if (string.IsNullOrWhiteSpace(pattern))
{
throw new ArgumentException("pattern 参数不能为空");
}
value = Regex.Replace(value, pattern, replacement, options);
return value;
}
}
字符串扩展使用
static void Main(string[] args)
{
string original = "NameNameemaNHello Name World!NameName";
Console.WriteLine($"原字符串:{original}\r\n");
Console.WriteLine($"移除指定字符串前后Name并重复匹配:{original.Trim("Name")}");
Console.WriteLine(Environment.NewLine);
Console.WriteLine($"移除指定字符串前后Name单次匹配:{original.Trim("Name", false)}");
Console.WriteLine(Environment.NewLine);
Console.WriteLine($"移除字符串开始Name并重复匹配:{original.TrimStart("Name")}");
Console.WriteLine(Environment.NewLine);
Console.WriteLine($"移除字符串开始Name不重复匹配:{original.TrimStart("Name", false)}");
Console.WriteLine(Environment.NewLine);
Console.WriteLine($"移除字符串结束Name并重复匹配:{original.TrimEnd("Name")}");
Console.WriteLine(Environment.NewLine);
Console.WriteLine($"移除字符串结束Name不重复匹配:{original.TrimEnd("Name", false)}");
Console.Read();
}
转载请注明:清风亦平凡 » C#使用正则表达式移除字串符前后指定的字符串