Files
tnb.server/common/Tnb.Common/Extension/TnbStringExtensions.cs

203 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text.RegularExpressions;
using JNPF.Common.Extension;
namespace System;
/// <summary>
/// 字符串<see cref="string"/>类型的扩展辅助操作类.
/// </summary>
public static class StringExtensions
{
#region ,Abp
/// <summary>
/// Removes first occurrence of the given prefixes from beginning of the given string.
/// </summary>
/// <param name="str">The string.</param>
/// <param name="preFixes">one or more prefix.</param>
/// <returns>Modified string or the same string if it has not any of given prefixes</returns>
public static string RemovePreFix(this string str, params string[] preFixes)
{
return str.RemovePreFix(StringComparison.Ordinal, preFixes);
}
/// <summary>
/// Removes first occurrence of the given prefixes from beginning of the given string.
/// </summary>
/// <param name="str">The string.</param>
/// <param name="comparisonType">String comparison type</param>
/// <param name="preFixes">one or more prefix.</param>
/// <returns>Modified string or the same string if it has not any of given prefixes</returns>
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;
}
/// <summary>
/// Removes first occurrence of the given postfixes from end of the given string.
/// </summary>
/// <param name="str">The string.</param>
/// <param name="postFixes">one or more postfix.</param>
/// <returns>Modified string or the same string if it has not any of given postfixes</returns>
public static string RemovePostFix(this string str, params string[] postFixes)
{
return str.RemovePostFix(StringComparison.Ordinal, postFixes);
}
/// <summary>
/// Removes first occurrence of the given postfixes from end of the given string.
/// </summary>
/// <param name="str">The string.</param>
/// <param name="comparisonType">String comparison type</param>
/// <param name="postFixes">one or more postfix.</param>
/// <returns>Modified string or the same string if it has not any of given postfixes</returns>
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;
}
/// <summary>
/// Gets a substring of a string from beginning of the string.
/// </summary>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="str"/> is null</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="len"/> is bigger that string's length</exception>
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);
}
/// <summary>
/// Gets a substring of a string from end of the string.
/// </summary>
/// <exception cref="ArgumentNullException">Thrown if <paramref name="str"/> is null</exception>
/// <exception cref="ArgumentException">Thrown if <paramref name="len"/> is bigger that string's length</exception>
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;
}
/// <summary>
/// 判断字符串是否为空为空时返回defaultVal不为空时返回自身或者Func
/// </summary>
public static string IfNullOrEmpty(this string? str, string defaultVal, Func<string, string>? 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);
}
/// <summary>
/// 将字符串首字母改成大写
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
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);
}
/// <summary>
/// 将camelCase,PascalCase,kebab-case,snake_case,sentence case分隔为单词列表并转成小写
/// </summary>
public static string[] SplitWord(this string str, bool toLower = true)
{
if (str == null) return Array.Empty<string>();
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
/// <summary>
/// 查找路径的上一级
/// </summary>
public static (string?, string) GetParent(this string str, char seperate)
{
int n = str.LastIndexOf(seperate);
if (n > 0)
{
return (str.Substring(0, n), str.Substring(n + 1));
}
else
{
return (null, str);
}
}
}