mirror of
https://github.com/shekhargulati/strman-java.git
synced 2026-10-20 14:34:04 +00:00
Added support for between function
This commit is contained in:
@@ -9,6 +9,7 @@ A Java 8 string manipulation library without any dependencies. It is inspired by
|
||||
* [append](https://github.com/shekhargulati/strman-java#appendvalue-strings)
|
||||
* [appendArray](https://github.com/shekhargulati/strman-java#appendarrayvalue-strings)
|
||||
* [at](https://github.com/shekhargulati/strman-java#atvalue-index)
|
||||
* [between](https://github.com/shekhargulati/strman-java#betweenvalue-start-end)
|
||||
|
||||
|
||||
## append(value, strings...)
|
||||
@@ -42,4 +43,13 @@ Gets the character at index.
|
||||
import static strman.Strman.at;
|
||||
String result = at("foobar", 0);
|
||||
// result = "f"
|
||||
```
|
||||
|
||||
## between(value, start, end)
|
||||
|
||||
Returns array with strings between `start` and `end`.
|
||||
|
||||
```java
|
||||
String[] parts = between("[abc][def]", "[", "]";
|
||||
// parts = ["abc","def"]
|
||||
```
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,9 @@ import org.junit.Test;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static strman.Strman.append;
|
||||
import static strman.Strman.appendArray;
|
||||
import static strman.Strman.at;
|
||||
import static strman.Strman.*;
|
||||
|
||||
public class StrmanTest {
|
||||
|
||||
@@ -45,4 +44,11 @@ public class StrmanTest {
|
||||
assertThat(at("foobar", 10), equalTo(Optional.empty()));
|
||||
assertThat(at("foobar", -10), equalTo(Optional.empty()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnArrayWithStringsBetweenStartAndEnd() throws Exception {
|
||||
assertThat(between("[abc][def]", "[", "]"), arrayContaining("abc", "def"));
|
||||
assertThat(between("<span>foo</span>", "<span>", "</span>"), arrayContaining("foo"));
|
||||
assertThat(between("<span>foo</span><span>bar</span>", "<span>", "</span>"), arrayContaining("foo", "bar"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user