字符串扩展方法SplitStr会去掉空字符串和每个分割字符串两端空格

This commit is contained in:
falcon 2022-03-29 16:14:07 +08:00
parent 8cb24bc6af
commit 3404365b0b
2 changed files with 7 additions and 1 deletions

View File

@ -16,6 +16,12 @@ namespace Falcon.SugarApi.Test
var str = $"a,b.c;de。fg";
var strArr = str.SplitStr();
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]);
str = null;
strArr = str.SplitStr();
Assert.IsNotNull(strArr);

View File

@ -25,6 +25,6 @@ namespace Falcon.SugarApi
/// </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[] { ',', '', ';', '', '.', '。' });
public static string[] SplitStr(this string? str) => str == null ? Array.Empty<string>() : str.Split(new char[] { ',', '', ';', '', '.', '。' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
}
}