Skip to content

Commit da06f5a

Browse files
committed
💾 ✔️ Feat, Test(ListHelper): Cut ending now available and init unit test.
1 parent 4f150a0 commit da06f5a

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
namespace Common.BasicHelper.Utils.Extensions;
2+
3+
[TestClass()]
4+
public class ListHelper_Tests
5+
{
6+
[TestMethod()]
7+
public void Test_ToCustomString()
8+
{
9+
Assert.AreEqual
10+
(
11+
new List<int>()
12+
{
13+
1, 2, 3, 4, 5, 6, 7,
14+
}.ToCustomString().Print(),
15+
"1,2,3,4,5,6,7"
16+
);
17+
18+
Assert.AreEqual
19+
(
20+
new List<int>()
21+
{
22+
1, 2, 3, 4, 5, 6, 7,
23+
}.ToCustomString(cutEnding: false).Print(),
24+
"1,2,3,4,5,6,7,"
25+
);
26+
}
27+
}

Common.BasicHelper/Utils/Extensions/ListHelper.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static class ListHelper
1212
/// <param name="list">列表</param>
1313
/// <param name="separater">分隔符</param>
1414
/// <returns>自定义字符串</returns>
15-
public static string ToCustomString<T>(this List<T> list, string separater = ",")
15+
public static string ToCustomString<T>(this List<T> list, string separater = ",", bool cutEnding = true)
1616
{
1717
var sb = new StringBuilder();
1818

@@ -22,7 +22,9 @@ public static string ToCustomString<T>(this List<T> list, string separater = ","
2222
sb.Append(separater);
2323
}
2424

25-
return sb.ToString();
25+
var result = sb.ToString();
26+
27+
return cutEnding ? result[0..(result.Length - separater.Length)] : result;
2628
}
2729

2830
}

0 commit comments

Comments
 (0)