Skip to content

Commit 3594042

Browse files
committed
Resolved shekhargulati#53
1 parent 6bbe0a9 commit 3594042

2 files changed

Lines changed: 52 additions & 3 deletions

File tree

src/main/java/strman/Strman.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,28 @@ public static String encode(final String value, final int digits, final int radi
11091109
return value.chars().mapToObj(ch -> leftPad(Integer.toString(ch, radix), "0", digits)).collect(joining());
11101110
}
11111111

1112+
1113+
/**
1114+
* Join concatenates all the elements of the strings array into a single String. The separator string is placed between elements in the resulting string.
1115+
*
1116+
* @param strings The input array to concatenate
1117+
* @param separator The separator to use
1118+
* @return Concatenated String
1119+
*/
1120+
public static String join(final String[] strings, final String separator) {
1121+
if (strings == null) {
1122+
throw new IllegalArgumentException("Input array 'strings' can't be null");
1123+
}
1124+
if (separator == null) {
1125+
throw new IllegalArgumentException("separator can't be null");
1126+
}
1127+
StringJoiner joiner = new StringJoiner(separator);
1128+
for (String el : strings) {
1129+
joiner.add(el);
1130+
}
1131+
return joiner.toString();
1132+
}
1133+
11121134
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
11131135
if (predicate.test(value)) {
11141136
throw new IllegalArgumentException(supplier.get());

src/test/java/strman/StrmanTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@ public void indexOf_shouldBeTrueWhenNeedleExistCaseSensitive() throws Exception
411411

412412
@Test
413413
public void inequal_shouldTestInequalityOfStrings() throws Exception {
414-
assertThat(inequal("a", "b"), equalTo(true));
415-
assertThat(inequal("a", "a"), equalTo(false));
416-
assertThat(inequal("0", "1"), equalTo(true));
414+
assertThat(unequal("a", "b"), equalTo(true));
415+
assertThat(unequal("a", "a"), equalTo(false));
416+
assertThat(unequal("0", "1"), equalTo(true));
417417
}
418418

419419
@Test
@@ -885,4 +885,31 @@ public void snakeCase_shouldConvertAStringToSnakecase() throws Exception {
885885
assertThat(String.format("%s should be foo_bar", el), toSnakeCase(el), is(equalTo("foo_bar"))));
886886

887887
}
888+
889+
@Test
890+
public void join_shouldJoinArrayOfStringIntoASingleString() throws Exception {
891+
String[] strings = {
892+
"hello",
893+
"world",
894+
"123"
895+
};
896+
assertThat(join(strings, ";"), is(equalTo("hello;world;123")));
897+
}
898+
899+
@Test(expected = IllegalArgumentException.class)
900+
public void join_shouldThrowIllegalArgumentExceptionWhenSeparatorIsNull() throws Exception {
901+
String[] strings = {
902+
"hello",
903+
"world",
904+
"123"
905+
};
906+
907+
join(strings, null);
908+
}
909+
910+
@Test
911+
public void join_shouldReturnEmptyStringWhenInputArrayIsEmpty() throws Exception {
912+
String[] emptyArray = {};
913+
assertThat(join(emptyArray, ","), is(equalTo("")));
914+
}
888915
}

0 commit comments

Comments
 (0)