using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; namespace Infrastructure.Extensions { public static class StringExtension { /// /// SQL条件拼接 /// /// /// /// public static string If(this string str, bool condition) { return condition ? str : string.Empty; } /// /// 判断是否为空 /// /// /// public static bool IfNotEmpty(this string str) { return !string.IsNullOrEmpty(str); } /// /// 注意:如果替换的旧值中有特殊符号,替换将会失败,解决办法 例如特殊符号是“(”: 要在调用本方法前加oldValue=oldValue.Replace("(","//("); /// /// /// /// /// public static string ReplaceFirst(this string input, string oldValue, string newValue) { Regex regEx = new Regex(oldValue, RegexOptions.Multiline); return regEx.Replace(input, newValue == null ? "" : newValue, 1); } } }