using System.Text.RegularExpressions;
using JNPF.Common.Extension;
namespace System;
///
/// 字符串类型的扩展辅助操作类.
///
public static class StringExtensions
{
#region 转换,来自Abp
///
/// Removes first occurrence of the given prefixes from beginning of the given string.
///
/// The string.
/// one or more prefix.
/// Modified string or the same string if it has not any of given prefixes
public static string RemovePreFix(this string str, params string[] preFixes)
{
return str.RemovePreFix(StringComparison.Ordinal, preFixes);
}
///
/// Removes first occurrence of the given prefixes from beginning of the given string.
///
/// The string.
/// String comparison type
/// one or more prefix.
/// Modified string or the same string if it has not any of given prefixes
public static string RemovePreFix(this string str, StringComparison comparisonType, params string[] preFixes)
{
if (str.IsNullOrEmpty())
{
return str;
}
if (preFixes.IsNullOrEmpty())
{
return str;
}
foreach (var preFix in preFixes)
{
if (str.StartsWith(preFix, comparisonType))
{
return str.Right(str.Length - preFix.Length);
}
}
return str;
}
///
/// Removes first occurrence of the given postfixes from end of the given string.
///
/// The string.
/// one or more postfix.
/// Modified string or the same string if it has not any of given postfixes
public static string RemovePostFix(this string str, params string[] postFixes)
{
return str.RemovePostFix(StringComparison.Ordinal, postFixes);
}
///
/// Removes first occurrence of the given postfixes from end of the given string.
///
/// The string.
/// String comparison type
/// one or more postfix.
/// Modified string or the same string if it has not any of given postfixes
public static string RemovePostFix(this string str, StringComparison comparisonType, params string[] postFixes)
{
if (str.IsNullOrEmpty())
{
return str;
}
if (postFixes.IsNullOrEmpty())
{
return str;
}
foreach (var postFix in postFixes)
{
if (str.EndsWith(postFix, comparisonType))
{
return str.Left(str.Length - postFix.Length);
}
}
return str;
}
///
/// Gets a substring of a string from beginning of the string.
///
/// Thrown if is null
/// Thrown if is bigger that string's length
public static string Left(this string str, int len)
{
if (str.Length < len)
{
throw new ArgumentException("len argument can not be bigger than given string's length!");
}
return str.Substring(0, len);
}
///
/// Gets a substring of a string from end of the string.
///
/// Thrown if is null
/// Thrown if is bigger that string's length
public static string Right(this string str, int len)
{
if (str.Length < len)
{
throw new ArgumentException("len argument can not be bigger than given string's length!");
}
return str.Substring(str.Length - len, len);
}
private static bool IsAllUpperCase(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (Char.IsLetter(input[i]) && !Char.IsUpper(input[i]))
{
return false;
}
}
return true;
}
///
/// 判断字符串是否为空,为空时返回defaultVal,不为空时返回自身或者Func
///
public static string IfNullOrEmpty(this string? str, string defaultVal, Func? result = null)
{
if (string.IsNullOrEmpty(str)) return defaultVal;
else if (result == null) return str;
else return result(str);
}
public static string Format(this string str, params object?[] args)
{
return string.Format(str, args);
}
///
/// 将字符串首字母改成大写
///
///
///
public static string UpperFirst(this string str)
{
if (string.IsNullOrWhiteSpace(str)) return str;
if (str.Length == 1)
{
return str.ToUpper();
}
return char.ToUpper(str[0]) + str.Substring(1);
}
///
/// 将camelCase,PascalCase,kebab-case,snake_case,sentence case分隔为单词列表,并转成小写
///
public static string[] SplitWord(this string str, bool toLower = true)
{
if (str == null) return Array.Empty();
if (string.IsNullOrWhiteSpace(str)) return new string[] { str };
if (str.Length == 1) return new string[] { str };
var q = Regex.Split(str, @"(?=\p{Lu}\p{Ll})|(?<=\p{Ll})(?=\p{Lu})|[-_ ]+").Where(u => u.Length > 0);
//useCurrentCulture ? char.ToLower(chars[i], CultureInfo.InvariantCulture) : char.ToLowerInvariant(chars[i]);
if (toLower) q = q.Select(u => u.ToLower());
return q.ToArray();
}
public static string ToSnake(this string str) => str.SplitWord().JoinAsString("_");
public static string ToKebab(this string str) => str.SplitWord().JoinAsString("-");
public static string ToPascal(this string str) => str.SplitWord().Select(a => a.UpperFirst()).JoinAsString("");
public static string ToCamel(this string str) => str.SplitWord().Select((a, i) => i == 0 ? a : a.UpperFirst()).JoinAsString("");
#endregion
}