From 4fb6934cea7639d18a8e5568cb302eecf57fa8c5 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Fri, 9 Dec 2022 11:17:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95SubstringEx=E5=92=8CSplit?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi.Test/StringExtendTest.cs | 91 +++++++++++++++++++----- Falcon.SugarApi/StringExtend.cs | 80 +++++++++++++++++---- 2 files changed, 140 insertions(+), 31 deletions(-) diff --git a/Falcon.SugarApi.Test/StringExtendTest.cs b/Falcon.SugarApi.Test/StringExtendTest.cs index 1c9779e..d506388 100644 --- a/Falcon.SugarApi.Test/StringExtendTest.cs +++ b/Falcon.SugarApi.Test/StringExtendTest.cs @@ -1,4 +1,7 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; +using MySqlX.XDevAPI.Common; +using System; +using System.Linq; namespace Falcon.SugarApi.Test { @@ -13,31 +16,31 @@ namespace Falcon.SugarApi.Test public void TestSplit() { var str = $"a,b.c;d,e。f;g"; var strArr = str.SplitStr(); - Assert.AreEqual(7, strArr.Length, "分割后长度应该为7"); + Assert.AreEqual(7,strArr.Length,"分割后长度应该为7"); str = "a,,, ,b ,c c,"; strArr = str.SplitStr(); - Assert.AreEqual(3, strArr.Length, "分割后长度应该为3"); - Assert.AreEqual("a", strArr[0]); - Assert.AreEqual("b", strArr[1]); - Assert.AreEqual("c c", strArr[2]); + Assert.AreEqual(3,strArr.Length,"分割后长度应该为3"); + Assert.AreEqual("a",strArr[0]); + Assert.AreEqual("b",strArr[1]); + Assert.AreEqual("c c",strArr[2]); str = null; strArr = str.SplitStr(); Assert.IsNotNull(strArr); - Assert.AreEqual(0, strArr.Length); + Assert.AreEqual(0,strArr.Length); //指定分隔符 str = $"a,b.c;d,e。f;g"; strArr = str.SplitStr(','); - Assert.AreEqual(2, strArr.Length, "分割后长度应该为7"); - Assert.AreEqual("a", strArr[0]); - Assert.AreEqual("b.c;d,e。f;g", strArr[1]); + Assert.AreEqual(2,strArr.Length,"分割后长度应该为7"); + Assert.AreEqual("a",strArr[0]); + Assert.AreEqual("b.c;d,e。f;g",strArr[1]); - strArr = str.SplitStr(',', '.'); - Assert.AreEqual(3, strArr.Length, "分割后长度应该为7"); - Assert.AreEqual("a", strArr[0]); - Assert.AreEqual("b", strArr[1]); - Assert.AreEqual("c;d,e。f;g", strArr[2]); + strArr = str.SplitStr(',','.'); + Assert.AreEqual(3,strArr.Length,"分割后长度应该为7"); + Assert.AreEqual("a",strArr[0]); + Assert.AreEqual("b",strArr[1]); + Assert.AreEqual("c;d,e。f;g",strArr[2]); } /// @@ -48,13 +51,67 @@ namespace Falcon.SugarApi.Test var str = ""; var r = ""; r = str.ToDefault("abc"); - Assert.AreEqual("abc", r); + Assert.AreEqual("abc",r); str = null; r = str.ToDefault("abc"); - Assert.AreEqual("abc", r); + Assert.AreEqual("abc",r); str = "123"; r = str.ToDefault("abc"); - Assert.AreEqual("123", r); + Assert.AreEqual("123",r); + } + + /// + /// 检索子字符串测试 + /// + [TestMethod] + public void TestSubstringEx() { + var str = "abcd33"; + string r; + r = str.SubstringEx(str.Length + 1,1); + Assert.IsTrue(r == ""); + r = str.SubstringEx(0,0); + Assert.IsTrue(r == ""); + r = str.SubstringEx(3,-1); + Assert.IsTrue(r == "c"); + r = str.SubstringEx(3,10); + Assert.IsTrue(r == "d33"); + r = str.SubstringEx(-1,2); + Assert.IsTrue(r == "a"); + r = str.SubstringEx(1,2); + Assert.IsTrue(r == "bc"); + + } + + /// + /// 按一定长度分割字符串测试 + /// + [TestMethod] + public void TestSplitByLength() { + string str; + string[] result; + + str = ""; + Assert.ThrowsException(() => { result = str.Split(5).ToArray(); }); + str = "abc"; + Assert.ThrowsException(() => { result = str.Split(0).ToArray(); }); + result = str.Split(1).ToArray(); + Assert.IsTrue(result.Length == 3); + Assert.IsTrue(result[0] == "a"); + Assert.IsTrue(result[1] == "b"); + Assert.IsTrue(result[2] == "c"); + + result = str.Split(2).ToArray(); + Assert.IsTrue(result.Length == 2); + Assert.IsTrue(result[0] == "ab"); + Assert.IsTrue(result[1] == "c"); + + result = str.Split(3).ToArray(); + Assert.IsTrue(result.Length == 1); + Assert.IsTrue(result[0] == "abc"); + + result = str.Split(4).ToArray(); + Assert.IsTrue(result.Length == 1); + Assert.IsTrue(result[0] == "abc"); } } } diff --git a/Falcon.SugarApi/StringExtend.cs b/Falcon.SugarApi/StringExtend.cs index e38a5c5..b8a800e 100644 --- a/Falcon.SugarApi/StringExtend.cs +++ b/Falcon.SugarApi/StringExtend.cs @@ -1,6 +1,9 @@ using System; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Globalization; +using System.Linq; +using System.Reflection.Metadata; namespace Falcon.SugarApi { @@ -28,7 +31,7 @@ namespace Falcon.SugarApi /// 字符串 /// 默认值 /// 字符串本身或默认值 - public static string? ToDefault(this string? str, [NotNull] string defaultVal) => str.IsNullOrEmpty() ? defaultVal : str; + public static string? ToDefault(this string? str,[NotNull] string defaultVal) => str.IsNullOrEmpty() ? defaultVal : str; /// /// 使用全角半角的逗号句号和分号分割字符串。 @@ -36,8 +39,8 @@ 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); + 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); /// @@ -47,12 +50,12 @@ namespace Falcon.SugarApi /// 时间格式 /// 区域特性.默认CultureInfo.InvariantCulture /// 时间 - 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; - if (dateTimeFormat.IsNullOrEmpty()) { + if(dateTimeFormat.IsNullOrEmpty()) { return DateTime.Parse(str); } - return DateTime.ParseExact(str, dateTimeFormat, culture); + return DateTime.ParseExact(str,dateTimeFormat,culture); } /// /// 尝试将特定格式字符串转换为DateTime类型 @@ -62,12 +65,11 @@ namespace Falcon.SugarApi /// 转换后的时间 /// 区域特性.默认CultureInfo.InvariantCulture /// 成功True,失败False - 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 { - dt = str.ToDateTime(dateTimeFormat, culture); + dt = str.ToDateTime(dateTimeFormat,culture); return true; - } - catch (Exception) { + } catch(Exception) { dt = default; return false; } @@ -80,14 +82,64 @@ namespace Falcon.SugarApi /// 日期时间格式 /// 是True,否False /// 参数为空 - public static bool IsDateFormat(this string str, string dateFormat) { - if (str.IsNullOrEmpty()) { + public static bool IsDateFormat(this string str,string dateFormat) { + if(str.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(str)); } - if (dateFormat.IsNullOrEmpty()) { + if(dateFormat.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(dateFormat)); } - return str.TryToDateTime(dateFormat, out var _); + return str.TryToDateTime(dateFormat,out var _); + } + + /// + /// 获取子字符串 + /// 如果start大于字符串长度或者length等于0则返回空字符串。 + /// 如果length小于0则返回start前面长度的字符串。 + /// 如果字符串不满足需要的长度要求则返回实际长度字符串。 + /// 如果start小于0,则返回从0开始的length+start长度字符串。 + /// + /// 要检索的字符串 + /// 开始位置 + /// 获取长度 + /// 子字符串 + /// 获取长度不满足要求 + public static string SubstringEx(this string str,int start,int length) { + if(start > str.Length || length == 0) { + return ""; + } + if(length < 0) { + start = start + length; + length = -length; + } + if(start < 0) { + length += start; + start = 0; + } + if(start + length > str.Length) { + return str.Substring(start); + } + return str.Substring(start,length); + } + + /// + /// 按照固定长度分割字符串 + /// + /// 要分割的字符串 + /// 分割长度 + /// 分割后的字符串枚举 + public static IEnumerable Split(this string str,int length) { + if(length <= 0) { + throw new Exception("分割长度必须大于0"); + } + if(str.IsNullOrEmpty()) { + throw new ArgumentNullException(nameof(str)); + } + int i = 0; + while(str.Length > i) { + yield return str.SubstringEx(i,length); + i += length; + } } } } \ No newline at end of file