增加String.SplitToStringList方法。

This commit is contained in:
Falcon 2024-12-03 11:23:59 +08:00
parent 66d22c5c08
commit cd93e01074
2 changed files with 36 additions and 0 deletions

View File

@ -129,5 +129,23 @@ namespace Falcon.SugarApi.Test
r = str.TrimString("abc","256");
Assert.AreEqual(r,"ffe222");
}
/// <summary>
/// SplitToStringList测试
/// </summary>
[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);
}
}
}

View File

@ -40,6 +40,24 @@ namespace Falcon.SugarApi
public static string[] SplitStr(this string? str,params char[] splitChars)
=> str.IsNullOrEmpty() ? Array.Empty<string>() : str.Split(splitChars.Length == 0 ? new char[] { ',','',';','','.','。' } : splitChars,StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
/// <summary>
/// 使用提供的分隔符分割字符串,返回分割后的字符串列表
/// </summary>
/// <param name="str">要分割的字符串</param>
/// <param name="splitChars">分割字符</param>
/// <returns>分割后的字符串列表</returns>
public static List<string> SplitToStringList(this string? str,params char[] splitChars) {
var list = new List<string>();
if(str.IsNullOrEmpty()) {
return list;
}
if(splitChars.Length == 0) {
splitChars = new char[] { ',','',';','','.','。' };
}
list.AddRange(str.Split(splitChars));
return list;
}
/// <summary>
/// 将字符串按格式转换为时间格式
/// </summary>