Skip to content

Commit bd2ecb3

Browse files
committed
resolved #195
1 parent 5e41955 commit bd2ecb3

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/CatLib.Core.Tests/Support/Util/StrTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,5 +367,16 @@ public void TestLevenshteinLargeThan255()
367367
Assert.AreEqual(256, builder.Length);
368368
Assert.AreEqual(-1, Str.Levenshtein(builder.ToString(), "world"));
369369
}
370+
371+
[TestMethod]
372+
public void TestJoinList()
373+
{
374+
var result = Str.JoinList(new[] {"hello", "world", "catlib"}, "/");
375+
376+
Assert.AreEqual("hello", result[0]);
377+
Assert.AreEqual("hello/world", result[1]);
378+
Assert.AreEqual("hello/world/catlib", result[2]);
379+
Assert.AreEqual(3, result.Length);
380+
}
370381
}
371382
}

src/CatLib.Core/Support/Util/Str.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,37 @@ int Min(int num1, int num2, int num3)
551551

552552
return p1[length2];
553553
}
554+
555+
/// <summary>
556+
/// Returns all sequential combination of the given array.
557+
/// </summary>
558+
/// <remarks>
559+
/// v[0] = "hello"
560+
/// v[1] = "world"
561+
/// var result = Str.JoinList(v, "/");
562+
/// result[0] == "hello";
563+
/// result[1] == "hello/world";
564+
/// </remarks>
565+
/// <param name="source">The source array.</param>
566+
/// <param name="separator">The separator.</param>
567+
/// <returns>The sequential combination array.</returns>
568+
public static string[] JoinList(string[] source, string separator = null)
569+
{
570+
Guard.Requires<ArgumentNullException>(source != null);
571+
var builder = new StringBuilder();
572+
for (var index = 1; index < source.Length; index++)
573+
{
574+
builder.Append(source[index - 1]);
575+
if (!string.IsNullOrEmpty(separator))
576+
{
577+
builder.Append(separator);
578+
}
579+
builder.Append(source[index]);
580+
source[index] = builder.ToString();
581+
builder.Remove(0, source[index].Length);
582+
}
583+
return source;
584+
}
554585
}
555586
}
556587

0 commit comments

Comments
 (0)