Skip to content

Commit

Permalink
字符串拓展迭代
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuDanK committed Jul 11, 2024
1 parent 83c3406 commit 39123a1
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions SageTools/Extension/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ public static string JoinWith(this IEnumerable<string> value, string separator)
return string.Join(separator, value);
}

/// <summary>
/// string.Join()拓展
/// </summary>
public static string JoinWith(this IEnumerable<char> value, string separator)
{
return string.Join(separator, value);
}

/// <summary>
/// string.Replace("oldValue","")
/// </summary>
Expand Down Expand Up @@ -717,11 +725,11 @@ public static byte[] ToBytes(this string str)
/// string转byte[]
/// </summary>
/// <param name="str">字符串</param>
/// <param name="theEncoding">需要的编码</param>
/// <param name="encoding">需要的编码</param>
/// <returns></returns>
public static byte[] ToBytes(this string str, Encoding theEncoding)
public static byte[] ToBytes(this string str, Encoding encoding)
{
return theEncoding.GetBytes(str);
return encoding.GetBytes(str);
}

/// <summary>
Expand Down Expand Up @@ -798,5 +806,30 @@ public static TEnum ToEnum<TEnum>(this string enumText) where TEnum : struct

return value;
}

/// <summary>
/// 将布尔值转换为字符串表示。
/// </summary>
/// <param name="bl">布尔值。</param>
/// <param name="trueStr">布尔值为 true 时的字符串表示,默认为 "是"。</param>
/// <param name="falseStr">布尔值为 false 时的字符串表示,默认为 "否"。</param>
/// <returns>根据布尔值返回相应的字符串表示。</returns>
public static string ToStringView(this bool bl, string trueStr = "是", string falseStr = "否")
{
return bl ? trueStr : falseStr;
}

/// <summary>
/// 将可空布尔值转换为字符串表示。
/// </summary>
/// <param name="bl">可空布尔值。</param>
/// <param name="trueStr">布尔值为 true 时的字符串表示,默认为 "是"。</param>
/// <param name="falseStr">布尔值为 false 时的字符串表示,默认为 "否"。</param>
/// <param name="nullStr">布尔值为 null 时的字符串表示,默认为空字符串。</param>
/// <returns>根据可空布尔值返回相应的字符串表示。</returns>
public static string ToStringView(this bool? bl, string trueStr = "是", string falseStr = "否", string nullStr = "")
{
return bl.HasValue ? bl.Value ? trueStr : falseStr : nullStr;
}
}
}

0 comments on commit 39123a1

Please sign in to comment.