字符串扩展方法SplitStr会去掉空字符串和每个分割字符串两端空格
This commit is contained in:
parent
8cb24bc6af
commit
3404365b0b
|
@ -16,6 +16,12 @@ namespace Falcon.SugarApi.Test
|
||||||
var str = $"a,b.c;d,e。f;g";
|
var str = $"a,b.c;d,e。f;g";
|
||||||
var strArr = str.SplitStr();
|
var strArr = str.SplitStr();
|
||||||
Assert.AreEqual(7, strArr.Length, "分割后长度应该为7");
|
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;
|
str = null;
|
||||||
strArr = str.SplitStr();
|
strArr = str.SplitStr();
|
||||||
Assert.IsNotNull(strArr);
|
Assert.IsNotNull(strArr);
|
||||||
|
|
|
@ -25,6 +25,6 @@ namespace Falcon.SugarApi
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="str">要分割的字符串</param>
|
/// <param name="str">要分割的字符串</param>
|
||||||
/// <returns>字符串数组。当str为null时返回空数组</returns>
|
/// <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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user