From cd93e010747c962cee2bb68a25259e760c88cefd Mon Sep 17 00:00:00 2001 From: Falcon <12919280+falconfly@user.noreply.gitee.com> Date: Tue, 3 Dec 2024 11:23:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0String.SplitToStringList?= =?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 | 18 ++++++++++++++++++ Falcon.SugarApi/StringExtend.cs | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/Falcon.SugarApi.Test/StringExtendTest.cs b/Falcon.SugarApi.Test/StringExtendTest.cs index c2b1258..cda7226 100644 --- a/Falcon.SugarApi.Test/StringExtendTest.cs +++ b/Falcon.SugarApi.Test/StringExtendTest.cs @@ -129,5 +129,23 @@ namespace Falcon.SugarApi.Test r = str.TrimString("abc","256"); Assert.AreEqual(r,"ffe222"); } + + /// + /// SplitToStringList测试 + /// + [TestMethod] + public void SplitToStringListTest() { + var str = "abc,123,ggg"; + var l = str.SplitToStringList(); + Assert.IsTrue(l.Count == 3); + Assert.IsTrue(l[0] == "abc"); + Assert.IsTrue(l[1] == "123"); + Assert.IsTrue(l[2] == "ggg"); + + str = ""; + l = str.SplitToStringList(); + Assert.IsTrue(l.Count == 0); + + } } } diff --git a/Falcon.SugarApi/StringExtend.cs b/Falcon.SugarApi/StringExtend.cs index ac97702..995f741 100644 --- a/Falcon.SugarApi/StringExtend.cs +++ b/Falcon.SugarApi/StringExtend.cs @@ -40,6 +40,24 @@ namespace Falcon.SugarApi 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 List SplitToStringList(this string? str,params char[] splitChars) { + var list = new List(); + if(str.IsNullOrEmpty()) { + return list; + } + if(splitChars.Length == 0) { + splitChars = new char[] { ',',',',';',';','.','。' }; + } + list.AddRange(str.Split(splitChars)); + return list; + } + /// /// 将字符串按格式转换为时间格式 ///