diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 616db25..5b73a42 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -976,6 +976,40 @@ public abstract class Strman { return append(_prefix, value, Optional.ofNullable(suffix).orElse(_prefix)); } + /** + * Transform to camelCase + * + * @param value The input String + * @return String in camelCase. + */ + public static String toCamelCase(final String value) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + String str = toStudlyCase(value); + return str.substring(0, 1).toLowerCase() + str.substring(1); + } + + /** + * Transform to StudlyCaps. + * + * @param value The input String + * @return String in StudlyCaps. + */ + public static String toStudlyCase(final String value) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + String[] words = collapseWhitespace(value.trim()).split("\\s*(_|-|\\s)\\s*"); + return Arrays.stream(words).filter(w -> !w.trim().isEmpty()).map(w -> head(w).toUpperCase() + tail(w)).collect(joining()); + } + + /** + * Return tail of the String + * + * @param value The input String + * @return String tail + */ + public static String tail(final String value) { + validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); + return last(value, value.length() - 1); + } public static String decode(final String value, final int digits, final int radix) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 364c08a..648cfe9 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -714,7 +714,7 @@ public class StrmanTest { } @Test - public void surround_shouldSurrondStringWithPrefixAndSuffix() throws Exception { + public void surround_shouldSurroundStringWithPrefixAndSuffix() throws Exception { assertThat(surround("foo", "bar", null), equalTo("barfoobar")); assertThat(surround("shekhar", "***", null), equalTo("***shekhar***")); assertThat(surround("", ">", null), equalTo(">>")); @@ -722,4 +722,22 @@ public class StrmanTest { assertThat(surround("f", null, null), equalTo("f")); assertThat(surround("div", "<", ">"), equalTo("
")); } + + @Test + public void toCamelCase_shouldConvertStringToCamelCase() throws Exception { + String[] fixture = { + "CamelCase", + "camelCase", + "Camel case", + "Camel case", + "camel Case", + "camel-case", + "-camel--case", + "camel_case", + " camel_case", + }; + Arrays.stream(fixture).forEach(el -> assertThat(String.format("toCameCase(%s) should be camelCase", el), toCamelCase(el), equalTo("camelCase"))); + + assertThat(toCamelCase("c"), equalTo("c")); + } } \ No newline at end of file