2021-12-25 12:05:30 +08:00

46 lines
1.4 KiB
C#

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