diff --git a/Falcon.SugarApi/StringExtend.cs b/Falcon.SugarApi/StringExtend.cs index b8a800e..6fed262 100644 --- a/Falcon.SugarApi/StringExtend.cs +++ b/Falcon.SugarApi/StringExtend.cs @@ -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 /// /// 字符串 /// 空或null为True,否则False - public static bool IsNullOrEmpty(this string? str) => str == null || string.IsNullOrEmpty(str); + public static bool IsNullOrEmpty(this string? str) => str==null||string.IsNullOrEmpty(str); /// /// 字符串是否不为空或null /// @@ -40,8 +38,7 @@ namespace Falcon.SugarApi /// /// 字符串数组。当str为null时返回空数组 public static string[] SplitStr(this string? str,params char[] splitChars) - => str.IsNullOrEmpty() ? Array.Empty() : str.Split(splitChars.Length == 0 ? new char[] { ',',',',';',';','.','。' } : splitChars,StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); - + => str.IsNullOrEmpty() ? Array.Empty() : str.Split(splitChars.Length==0 ? new char[] { ',',',',';',';','.','。' } : splitChars,StringSplitOptions.RemoveEmptyEntries|StringSplitOptions.TrimEntries); /// /// 将字符串按格式转换为时间格式 @@ -51,7 +48,7 @@ namespace Falcon.SugarApi /// 区域特性.默认CultureInfo.InvariantCulture /// 时间 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 /// 成功True,失败False 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; } } - + /// + /// 尝试将特定格式字符串转换为DateTime类型 + /// + /// 时间字符串 + /// 转换后的时间 + /// 时间格式 + /// 区域特性.默认CultureInfo.InvariantCulture + /// 成功True,失败False + public static bool TryToDateTime(this string str,out DateTime dt,string dateTimeFormat = "",CultureInfo? culture = null) + => str.TryToDateTime(dateTimeFormat,out dt); /// /// 返回字符串是否为指定格式的日期时间格式 /// @@ -105,18 +112,18 @@ namespace Falcon.SugarApi /// 子字符串 /// 获取长度不满足要求 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 /// 分割长度 /// 分割后的字符串枚举 public static IEnumerable 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; } } }