From 59da6a956f3e27b4d550829b658408fa47943182 Mon Sep 17 00:00:00 2001 From: Shekhar Gulati Date: Wed, 4 May 2016 19:08:21 +0530 Subject: [PATCH] Added support for `chars` function --- README.md | 20 +++++++++++++++++++- src/main/java/strman/Strman.java | 24 ++++++++++++++++++++++-- src/test/java/strman/StrmanTest.java | 7 ++++++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a017691..2f416db 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,22 @@ Returns array with strings between `start` and `end`. import static strman.Strman.between; String[] parts = between("[abc][def]", "[", "]"; // parts = ["abc","def"] -``` \ No newline at end of file +``` + +## chars(value) + +Returns a String array consisting of the characters in the String. + +```java +final String title = "title"; +char[] result = chars(title); +// result = ["t", "i", "t", "l", "e"] +``` + + + + +## Other functions that can be added + +1. head +2. tail \ No newline at end of file diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index a8f9a36..e3df10a 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -18,7 +18,7 @@ public interface Strman { * @param appends an array of strings to append * @return full String */ - static String append(final String value, final String... appends) throws IllegalArgumentException { + static String append(final String value, final String... appends) { return appendArray(value, appends); } @@ -79,9 +79,29 @@ public interface Strman { return Arrays.stream(parts).map(subPart -> subPart.substring(subPart.indexOf(start) + start.length())).toArray(String[]::new); } - static void validate(String value, Supplier supplier) { + static void validate(final String value, final Supplier supplier) { if (value == null) { throw new IllegalArgumentException(supplier.get()); } } + + /** + * Returns a String array consisting of the characters in the String. + * + * @param value input + * @return character array + */ + static String[] chars(final String value) { + /** + * The other implementation of this could be using String's split method + * String[] chars = value.split("") + */ + validate(value, () -> "'value' should be not null."); + int length = value.length(); + String[] result = new String[length]; + for (int i = 0; i < length; i++) { + result[i] = at(value, i).get(); + } + return result; + } } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index b8b173a..0c5825f 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -6,7 +6,6 @@ import java.util.Optional; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; -import static org.hamcrest.collection.IsArrayWithSize.emptyArray; import static org.junit.Assert.assertThat; import static strman.Strman.*; @@ -58,4 +57,10 @@ public class StrmanTest { assertThat(between("[abc][def]", "{", "}"), arrayContaining("[abc][def]")); assertThat(between("", "{", "}"), arrayContaining("")); } + + @Test + public void charsShouldReturnAllCharactersInString() throws Exception { + final String title = "title"; + assertThat(chars(title), equalTo(new String[]{"t", "i", "t", "l", "e"})); + } } \ No newline at end of file