From b606113d63774c9bb2aa223de97371b4a1613efe Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Tue, 22 Mar 2022 16:53:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AD=97=E7=AC=A6=E4=B8=B2?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95SplitStr=E7=94=A8=E6=A0=87?= =?UTF-8?q?=E5=87=86=E5=85=A8=E8=A7=92=E5=8D=8A=E8=A7=92=E9=80=97=E5=8F=B7?= =?UTF-8?q?=E5=8F=A5=E5=8F=B7=E5=92=8C=E5=88=86=E5=8F=B7=E5=88=86=E5=89=B2?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Falcon.SugarApi.Test/StringExtendTest.cs | 25 ++++++++++++++++++++++++ Falcon.SugarApi/StringExtend.cs | 13 ++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 Falcon.SugarApi.Test/StringExtendTest.cs diff --git a/Falcon.SugarApi.Test/StringExtendTest.cs b/Falcon.SugarApi.Test/StringExtendTest.cs new file mode 100644 index 0000000..2613f83 --- /dev/null +++ b/Falcon.SugarApi.Test/StringExtendTest.cs @@ -0,0 +1,25 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Falcon.SugarApi; + +namespace Falcon.SugarApi.Test +{ + [TestClass] + public class StringExtendTest + { + [TestMethod] + public void TestSplit() { + var str = $"a,b.c;d,e。f;g"; + var strArr = str.SplitStr(); + Assert.AreEqual(7, strArr.Length, "分割后长度应该为7"); + str = null; + strArr = str.SplitStr(); + Assert.IsNotNull(strArr); + Assert.AreEqual(0, strArr.Length); + } + } +} diff --git a/Falcon.SugarApi/StringExtend.cs b/Falcon.SugarApi/StringExtend.cs index 3b56fdd..cc4cb27 100644 --- a/Falcon.SugarApi/StringExtend.cs +++ b/Falcon.SugarApi/StringExtend.cs @@ -1,9 +1,11 @@ -namespace Falcon.SugarApi +using System; + +namespace Falcon.SugarApi { /// /// 字符串扩展 /// - internal static class StringExtend + public static class StringExtend { /// /// 字符串是否为空或null @@ -17,5 +19,12 @@ /// 字符串 /// 与IsNullOrEmpty相反 public static bool IsNotNullOrEmpty(this string str) => !str.IsNullOrEmpty(); + + /// + /// 使用全角半角的逗号句号和分号分割字符串。 + /// + /// 要分割的字符串 + /// 字符串数组。当str为null时返回空数组 + public static string[] SplitStr(this string? str) => str == null ? Array.Empty() : str.Split(new char[] { ',', ',', ';', ';', '.', '。' }); } }