增加字符串扩展方法SplitStr用标准全角半角逗号句号和分号分割字符串

This commit is contained in:
falcon 2022-03-22 16:53:24 +08:00
parent e6934d6041
commit b606113d63
2 changed files with 36 additions and 2 deletions

View File

@ -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;de。fg";
var strArr = str.SplitStr();
Assert.AreEqual(7, strArr.Length, "分割后长度应该为7");
str = null;
strArr = str.SplitStr();
Assert.IsNotNull(strArr);
Assert.AreEqual(0, strArr.Length);
}
}
}

View File

@ -1,9 +1,11 @@
namespace Falcon.SugarApi
using System;
namespace Falcon.SugarApi
{
/// <summary>
/// 字符串扩展
/// </summary>
internal static class StringExtend
public static class StringExtend
{
/// <summary>
/// 字符串是否为空或null
@ -17,5 +19,12 @@
/// <param name="str">字符串</param>
/// <returns>与IsNullOrEmpty相反</returns>
public static bool IsNotNullOrEmpty(this string str) => !str.IsNullOrEmpty();
/// <summary>
/// 使用全角半角的逗号句号和分号分割字符串。
/// </summary>
/// <param name="str">要分割的字符串</param>
/// <returns>字符串数组。当str为null时返回空数组</returns>
public static string[] SplitStr(this string? str) => str == null ? Array.Empty<string>() : str.Split(new char[] { ',', '', ';', '', '.', '。' });
}
}