init commit
This commit is contained in:
48
20220330_Vote/Ewide.Core/Util/StringExtensions.cs
Normal file
48
20220330_Vote/Ewide.Core/Util/StringExtensions.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public static class StringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 下划线转为驼峰
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="initialsUppder"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToCamelCase(this string str, char split = '_', bool initialsUppder = true)
|
||||
{
|
||||
var _ = string.Join("",
|
||||
str.Split(split).Select(p => p.Length > 1 ?
|
||||
p.First().ToString().ToUpper() + p[1..] : p.ToUpper()
|
||||
)
|
||||
);
|
||||
|
||||
if (!initialsUppder && _.Length > 1)
|
||||
{
|
||||
_ = _.First().ToString().ToUpper() + _[1..];
|
||||
}
|
||||
|
||||
return _;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 驼峰转为下划线
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToUnderScoreCase(this string str)
|
||||
{
|
||||
var _ = new Regex(@"[A-Z]").Replace(str, "_$0");
|
||||
|
||||
if (_.Length > str.Length)
|
||||
{
|
||||
_ = _[1..];
|
||||
}
|
||||
|
||||
return _.ToLower();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user