Added support for between function

This commit is contained in:
Shekhar Gulati
2016-05-03 20:26:38 +05:30
parent a4bb84fd25
commit 94b0082fb0
3 changed files with 46 additions and 6 deletions
+27 -3
View File
@@ -1,7 +1,9 @@
package strman;
import java.util.Arrays;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.function.Supplier;
/**
* A String manipulation library without any dependencies
@@ -28,9 +30,7 @@ public interface Strman {
* @return full String
*/
static String appendArray(final String value, final String[] appends) {
if (value == null) {
throw new IllegalArgumentException("'value' should be not null.");
}
validate(value, () -> "'value' should be not null.");
if (appends == null || appends.length == 0) {
return value;
}
@@ -60,4 +60,28 @@ public interface Strman {
}
return (index < length && index >= 0) ? Optional.of(String.valueOf(value.charAt(index))) : Optional.empty();
}
/**
* Returns an array with strings between start and end.
*
* @param value
* @param start
* @param end
* @return Array containing different parts between start and end.
*/
static String[] between(final String value, final String start, final String end) {
validate(value, () -> "'value' should be not null.");
validate(start, () -> "'start' should be not null.");
validate(end, () -> "'end' should be not null.");
String[] parts = value.split(end);
return Arrays.stream(parts).map(subPart -> subPart.substring(subPart.indexOf(start) + start.length())).toArray(String[]::new);
}
static void validate(String value, Supplier<String> supplier) {
if (value == null) {
throw new IllegalArgumentException(supplier.get());
}
}
}