增加了新的string.TryToDateTime方法。

This commit is contained in:
falcon 2023-01-04 16:13:05 +08:00
parent 785457a5f4
commit 819a5657e8

View File

@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq;
using System.Reflection.Metadata;
namespace Falcon.SugarApi
{
@ -17,7 +15,7 @@ namespace Falcon.SugarApi
/// </summary>
/// <param name="str">字符串</param>
/// <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>
/// 字符串是否不为空或null
/// </summary>
@ -40,8 +38,7 @@ namespace Falcon.SugarApi
/// <param name="splitChars"></param>
/// <returns>字符串数组。当str为null时返回空数组</returns>
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>
/// 将字符串按格式转换为时间格式
@ -51,7 +48,7 @@ namespace Falcon.SugarApi
/// <param name="culture">区域特性.默认CultureInfo.InvariantCulture</param>
/// <returns>时间</returns>
public static DateTime ToDateTime(this string str,string dateTimeFormat = "",CultureInfo? culture = null) {
culture ??= CultureInfo.InvariantCulture;
culture??=CultureInfo.InvariantCulture;
if(dateTimeFormat.IsNullOrEmpty()) {
return DateTime.Parse(str);
}
@ -67,14 +64,24 @@ namespace Falcon.SugarApi
/// <returns>成功True失败False</returns>
public static bool TryToDateTime(this string str,string dateTimeFormat,out DateTime dt,CultureInfo? culture = null) {
try {
dt = str.ToDateTime(dateTimeFormat,culture);
dt=str.ToDateTime(dateTimeFormat,culture);
return true;
} catch(Exception) {
dt = default;
}
catch(Exception) {
dt=default;
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>
@ -105,18 +112,18 @@ namespace Falcon.SugarApi
/// <returns>子字符串</returns>
/// <exception cref="Exception">获取长度不满足要求</exception>
public static string SubstringEx(this string str,int start,int length) {
if(start > str.Length || length == 0) {
if(start>str.Length||length==0) {
return "";
}
if(length < 0) {
start = start + length;
length = -length;
if(length<0) {
start=start+length;
length=-length;
}
if(start < 0) {
length += start;
start = 0;
if(start<0) {
length+=start;
start=0;
}
if(start + length > str.Length) {
if(start+length>str.Length) {
return str.Substring(start);
}
return str.Substring(start,length);
@ -129,16 +136,16 @@ namespace Falcon.SugarApi
/// <param name="length">分割长度</param>
/// <returns>分割后的字符串枚举</returns>
public static IEnumerable<string> Split(this string str,int length) {
if(length <= 0) {
if(length<=0) {
throw new Exception("分割长度必须大于0");
}
if(str.IsNullOrEmpty()) {
throw new ArgumentNullException(nameof(str));
}
int i = 0;
while(str.Length > i) {
while(str.Length>i) {
yield return str.SubstringEx(i,length);
i += length;
i+=length;
}
}
}