Soul2的通用c#类库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
4.3 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Soul2.General.utils
{
/// <summary>
/// 字符串工具类
/// By Soul2
/// </summary>
public static class StringUtils
{
/// <summary>
/// 判断空值(null或"")
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool isEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
public static bool isNotEmpty(this string str)
{
return !string.IsNullOrEmpty(str);
}
/// <summary>
/// 判断空值(null、""或只有空格)
/// </summary>
/// <param name="str">被判断的字符串</param>
/// <returns></returns>
public static bool isNotBlank(this string str) { return !string.IsNullOrWhiteSpace(str); }
public static bool IsBlank(this string str) { return string.IsNullOrWhiteSpace(str); }
/// <summary>
/// 任何一个为空
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
public static bool isAnyBlank(params string[] strs)
{
foreach (var str in strs)
{
if (string.IsNullOrWhiteSpace(str))
{
return true;
}
}
return false;
}
/// <summary>
/// 任何一个为空
/// 空格属于非空
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
public static bool isAnyEmpty(params string[] strs)
{
foreach (var str in strs)
{
if (string.IsNullOrEmpty(str))
{
return true;
}
}
return false;
}
/// <summary>
/// 任何一个不为空
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
public static bool isNoneBlank(params string[] strs)
{
foreach (var str in strs)
{
if (string.IsNullOrWhiteSpace(str))
{
return false;
}
}
return true;
}
/// <summary>
/// 任何一个不为空
/// 空格属于非空
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
public static bool isNoneEmpty(params string[] strs)
{
foreach (var str in strs)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
}
return true;
}
/// <summary>
/// 如果不以val结尾,则添加到结尾
/// </summary>
/// <param name="original"></param>
/// <param name="str"></param>
public static void appendIfMissing(this string original, string str)
{
if (!original.EndsWith(str))
{
original += str;
}
}
/// <summary>
/// 如果不以val开头,则添加到开头
/// </summary>
/// <param name="original"></param>
/// <param name="str"></param>
public static void prependIfMissing(this string original, string str)
{
if (!original.StartsWith(str))
{
original = str + original;
}
}
/// <summary>
/// 翻转
/// 例:"123"->"321"
/// </summary>
/// <param name="original"></param>
/// <returns></returns>
public static string flip(this string original)
{
char[] chars = original.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
public static string subStrWith(this string original, int begin, int end)
{
if (begin < 0 || end > original.Length || begin > end)
{
throw new ArgumentException("Invalid input parameters");
}
return original.Substring(begin, end - begin);
}
}
}