mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-20 06:24:05 +00:00
Resolved #53
This commit is contained in:
@@ -1109,6 +1109,28 @@ public abstract class Strman {
|
||||
return value.chars().mapToObj(ch -> leftPad(Integer.toString(ch, radix), "0", digits)).collect(joining());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Join concatenates all the elements of the strings array into a single String. The separator string is placed between elements in the resulting string.
|
||||
*
|
||||
* @param strings The input array to concatenate
|
||||
* @param separator The separator to use
|
||||
* @return Concatenated String
|
||||
*/
|
||||
public static String join(final String[] strings, final String separator) {
|
||||
if (strings == null) {
|
||||
throw new IllegalArgumentException("Input array 'strings' can't be null");
|
||||
}
|
||||
if (separator == null) {
|
||||
throw new IllegalArgumentException("separator can't be null");
|
||||
}
|
||||
StringJoiner joiner = new StringJoiner(separator);
|
||||
for (String el : strings) {
|
||||
joiner.add(el);
|
||||
}
|
||||
return joiner.toString();
|
||||
}
|
||||
|
||||
private static void validate(String value, Predicate<String> predicate, final Supplier<String> supplier) {
|
||||
if (predicate.test(value)) {
|
||||
throw new IllegalArgumentException(supplier.get());
|
||||
|
||||
@@ -411,9 +411,9 @@ public class StrmanTest {
|
||||
|
||||
@Test
|
||||
public void inequal_shouldTestInequalityOfStrings() throws Exception {
|
||||
assertThat(inequal("a", "b"), equalTo(true));
|
||||
assertThat(inequal("a", "a"), equalTo(false));
|
||||
assertThat(inequal("0", "1"), equalTo(true));
|
||||
assertThat(unequal("a", "b"), equalTo(true));
|
||||
assertThat(unequal("a", "a"), equalTo(false));
|
||||
assertThat(unequal("0", "1"), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -885,4 +885,31 @@ public class StrmanTest {
|
||||
assertThat(String.format("%s should be foo_bar", el), toSnakeCase(el), is(equalTo("foo_bar"))));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void join_shouldJoinArrayOfStringIntoASingleString() throws Exception {
|
||||
String[] strings = {
|
||||
"hello",
|
||||
"world",
|
||||
"123"
|
||||
};
|
||||
assertThat(join(strings, ";"), is(equalTo("hello;world;123")));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void join_shouldThrowIllegalArgumentExceptionWhenSeparatorIsNull() throws Exception {
|
||||
String[] strings = {
|
||||
"hello",
|
||||
"world",
|
||||
"123"
|
||||
};
|
||||
|
||||
join(strings, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void join_shouldReturnEmptyStringWhenInputArrayIsEmpty() throws Exception {
|
||||
String[] emptyArray = {};
|
||||
assertThat(join(emptyArray, ","), is(equalTo("")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user