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