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; + } + /// /// 将字符串按格式转换为时间格式 ///