增加了新的string.TryToDateTime方法。
This commit is contained in:
parent
785457a5f4
commit
819a5657e8
|
@ -2,8 +2,6 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics.CodeAnalysis;
|
using System.Diagnostics.CodeAnalysis;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection.Metadata;
|
|
||||||
|
|
||||||
namespace Falcon.SugarApi
|
namespace Falcon.SugarApi
|
||||||
{
|
{
|
||||||
|
@ -17,7 +15,7 @@ namespace Falcon.SugarApi
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="str">字符串</param>
|
/// <param name="str">字符串</param>
|
||||||
/// <returns>空或null为True,否则False</returns>
|
/// <returns>空或null为True,否则False</returns>
|
||||||
public static bool IsNullOrEmpty(this string? str) => str == null || string.IsNullOrEmpty(str);
|
public static bool IsNullOrEmpty(this string? str) => str==null||string.IsNullOrEmpty(str);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 字符串是否不为空或null
|
/// 字符串是否不为空或null
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -40,8 +38,7 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="splitChars"></param>
|
/// <param name="splitChars"></param>
|
||||||
/// <returns>字符串数组。当str为null时返回空数组</returns>
|
/// <returns>字符串数组。当str为null时返回空数组</returns>
|
||||||
public static string[] SplitStr(this string? str,params char[] splitChars)
|
public static string[] SplitStr(this string? str,params char[] splitChars)
|
||||||
=> str.IsNullOrEmpty() ? Array.Empty<string>() : str.Split(splitChars.Length == 0 ? new char[] { ',',',',';',';','.','。' } : splitChars,StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
=> str.IsNullOrEmpty() ? Array.Empty<string>() : str.Split(splitChars.Length==0 ? new char[] { ',',',',';',';','.','。' } : splitChars,StringSplitOptions.RemoveEmptyEntries|StringSplitOptions.TrimEntries);
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将字符串按格式转换为时间格式
|
/// 将字符串按格式转换为时间格式
|
||||||
|
@ -51,7 +48,7 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="culture">区域特性.默认CultureInfo.InvariantCulture</param>
|
/// <param name="culture">区域特性.默认CultureInfo.InvariantCulture</param>
|
||||||
/// <returns>时间</returns>
|
/// <returns>时间</returns>
|
||||||
public static DateTime ToDateTime(this string str,string dateTimeFormat = "",CultureInfo? culture = null) {
|
public static DateTime ToDateTime(this string str,string dateTimeFormat = "",CultureInfo? culture = null) {
|
||||||
culture ??= CultureInfo.InvariantCulture;
|
culture??=CultureInfo.InvariantCulture;
|
||||||
if(dateTimeFormat.IsNullOrEmpty()) {
|
if(dateTimeFormat.IsNullOrEmpty()) {
|
||||||
return DateTime.Parse(str);
|
return DateTime.Parse(str);
|
||||||
}
|
}
|
||||||
|
@ -67,14 +64,24 @@ namespace Falcon.SugarApi
|
||||||
/// <returns>成功True,失败False</returns>
|
/// <returns>成功True,失败False</returns>
|
||||||
public static bool TryToDateTime(this string str,string dateTimeFormat,out DateTime dt,CultureInfo? culture = null) {
|
public static bool TryToDateTime(this string str,string dateTimeFormat,out DateTime dt,CultureInfo? culture = null) {
|
||||||
try {
|
try {
|
||||||
dt = str.ToDateTime(dateTimeFormat,culture);
|
dt=str.ToDateTime(dateTimeFormat,culture);
|
||||||
return true;
|
return true;
|
||||||
} catch(Exception) {
|
}
|
||||||
dt = default;
|
catch(Exception) {
|
||||||
|
dt=default;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 尝试将特定格式字符串转换为DateTime类型
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="str">时间字符串</param>
|
||||||
|
/// <param name="dt">转换后的时间</param>
|
||||||
|
/// <param name="dateTimeFormat">时间格式</param>
|
||||||
|
/// <param name="culture">区域特性.默认CultureInfo.InvariantCulture</param>
|
||||||
|
/// <returns>成功True,失败False</returns>
|
||||||
|
public static bool TryToDateTime(this string str,out DateTime dt,string dateTimeFormat = "",CultureInfo? culture = null)
|
||||||
|
=> str.TryToDateTime(dateTimeFormat,out dt);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 返回字符串是否为指定格式的日期时间格式
|
/// 返回字符串是否为指定格式的日期时间格式
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -105,18 +112,18 @@ namespace Falcon.SugarApi
|
||||||
/// <returns>子字符串</returns>
|
/// <returns>子字符串</returns>
|
||||||
/// <exception cref="Exception">获取长度不满足要求</exception>
|
/// <exception cref="Exception">获取长度不满足要求</exception>
|
||||||
public static string SubstringEx(this string str,int start,int length) {
|
public static string SubstringEx(this string str,int start,int length) {
|
||||||
if(start > str.Length || length == 0) {
|
if(start>str.Length||length==0) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if(length < 0) {
|
if(length<0) {
|
||||||
start = start + length;
|
start=start+length;
|
||||||
length = -length;
|
length=-length;
|
||||||
}
|
}
|
||||||
if(start < 0) {
|
if(start<0) {
|
||||||
length += start;
|
length+=start;
|
||||||
start = 0;
|
start=0;
|
||||||
}
|
}
|
||||||
if(start + length > str.Length) {
|
if(start+length>str.Length) {
|
||||||
return str.Substring(start);
|
return str.Substring(start);
|
||||||
}
|
}
|
||||||
return str.Substring(start,length);
|
return str.Substring(start,length);
|
||||||
|
@ -129,16 +136,16 @@ namespace Falcon.SugarApi
|
||||||
/// <param name="length">分割长度</param>
|
/// <param name="length">分割长度</param>
|
||||||
/// <returns>分割后的字符串枚举</returns>
|
/// <returns>分割后的字符串枚举</returns>
|
||||||
public static IEnumerable<string> Split(this string str,int length) {
|
public static IEnumerable<string> Split(this string str,int length) {
|
||||||
if(length <= 0) {
|
if(length<=0) {
|
||||||
throw new Exception("分割长度必须大于0");
|
throw new Exception("分割长度必须大于0");
|
||||||
}
|
}
|
||||||
if(str.IsNullOrEmpty()) {
|
if(str.IsNullOrEmpty()) {
|
||||||
throw new ArgumentNullException(nameof(str));
|
throw new ArgumentNullException(nameof(str));
|
||||||
}
|
}
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(str.Length > i) {
|
while(str.Length>i) {
|
||||||
yield return str.SubstringEx(i,length);
|
yield return str.SubstringEx(i,length);
|
||||||
i += length;
|
i+=length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user