BAEL-5013 Convert String to Camel Case (#10947)

* BAEL-5004 Fixed overflow issues for Large numbers

* BAEL-5013 Test cases

* BAEL-5013 Convert to String solutions

* BAEL-5013 convert to camel case using regex

* BAEL-5004 Fixed overflow issues for Large numbers

* BAEL-5013 Convert to Camel Case using Regex

* BAEL-5013 Review changes

* BAEL5013 cleanup

* BAEL5013 review changes

* BAEL5013 continuation index to 2 spaces

* BAEL-5013 fixed test failure

* BAEL-5013 Review changes

Co-authored-by: Remy Ohajinwa <remy.ohajinwa@interswitchgroup.com>
This commit is contained in:
Remy Ohajinwa 2021-07-18 04:41:05 -05:00 committed by GitHub
parent 17814a1468
commit d387500d53
3 changed files with 252 additions and 0 deletions

View File

@ -33,7 +33,39 @@
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>${icu.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<icu.version>64.2</icu.version>
<assertj.version>3.12.2</assertj.version>
<commons-text.version>1.9</commons-text.version>
<guava.version>30.1.1-jre</guava.version>
</properties>
<build>
<finalName>core-java-string-conversions-2</finalName>

View File

@ -0,0 +1,109 @@
package com.baeldung.stringtocamelcase;
import com.google.common.base.CaseFormat;
import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.text.BreakIterator;
import org.apache.commons.text.CaseUtils;
import org.apache.commons.text.WordUtils;
import java.util.Arrays;
import java.util.stream.Collectors;
public class StringToCamelCase {
public static String toCamelCaseByIteration(String text, char delimiter) {
if (text == null || text.isEmpty()) {
return text;
}
boolean shouldConvertNextCharToLower = true;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
char currentChar = text.charAt(i);
if (currentChar == delimiter) {
shouldConvertNextCharToLower = false;
} else if (shouldConvertNextCharToLower) {
builder.append(Character.toLowerCase(currentChar));
} else {
builder.append(Character.toUpperCase(currentChar));
shouldConvertNextCharToLower = true;
}
}
return builder.toString();
}
public static String toCamelCaseBySplitting(String text, String delimiter) {
if (text == null || text.isEmpty()) {
return text;
}
String[] words = text.split(delimiter);
StringBuilder builder = new StringBuilder();
for (int i = 0, wordsLength = words.length; i < wordsLength; i++) {
String word = words[i];
if (i == 0) {
//Make the first word all lowercase
word = word.isEmpty() ? word : word.toLowerCase();
} else {
//Convert the first character to Uppercase and others to lowercase
// e.g sTRING =====> String
word = word.isEmpty() ? word : Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase();
}
builder.append(word);
}
return builder.toString();
}
public static String toCamelCaseBySplittingUsingStreams(String text, String delimiter) {
if (text == null || text.isEmpty()) {
return text;
}
String[] words = text.split(delimiter);
//Convert the first word to lowercase and then every
//other word to Title Case.
String firstWord = words[0].toLowerCase();
String otherWords = Arrays.stream(words, 1, words.length)
.filter(word -> !word.isEmpty())
.map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase())
.collect(Collectors.joining(""));
return firstWord + otherWords;
}
public static String toCamelCaseByRegex(String text) {
StringBuilder builder = new StringBuilder();
String[] words = text.split("[\\W_]+");
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (i == 0) {
word = word.isEmpty() ? word : word.toLowerCase();
} else {
word = word.isEmpty() ? word : Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase();
}
builder.append(word);
}
return builder.toString();
}
//Third-Party Libraries
public static String toCamelCaseUsingICU4J(String text, String delimiter) {
if (text == null || text.isEmpty()) {
return text;
}
text = UCharacter.toTitleCase(text, BreakIterator.getTitleInstance()).replaceAll(delimiter, "");
StringBuilder builder = new StringBuilder(text);
builder.setCharAt(0, Character.toLowerCase(text.charAt(0)));
return builder.toString();
}
public static String toCamelCaseUsingGuava(String text, String delimiter) {
String toUpperUnderscore = text.toUpperCase().replaceAll(delimiter, "_");
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, toUpperUnderscore);
}
public static String toCamelCaseUsingApacheCommons(String text, char delimiter) {
text = WordUtils.capitalizeFully(text, delimiter).replaceAll(String.valueOf(delimiter), "");
StringBuilder builder = new StringBuilder(text);
builder.setCharAt(0, Character.toLowerCase(text.charAt(0)));
return builder.toString();
}
}

View File

@ -0,0 +1,111 @@
package com.baeldung.stringtocamelcase;
import com.google.common.base.CaseFormat;
import org.apache.commons.text.CaseUtils;
import org.junit.Test;
import static com.baeldung.stringtocamelcase.StringToCamelCase.*;
import static org.assertj.core.api.Assertions.*;
public class StringToCamelCaseUnitTest {
@Test
public void givenStringWithWhiteSpaces_WhenToCamelCaseByIteration_ThenReturnCamelCase() {
assertThat(toCamelCaseByIteration("THIS STRING SHOULD BE IN CAMEL CASE", ' ')).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithUnderscores_WhenToCamelCaseByIteration_ThenReturnCamelCase() {
assertThat(toCamelCaseByIteration("THIS_STRING_SHOULD_BE_IN_CAMEL_CASE", '_')).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithWhiteSpaces_WhenToCamelCaseBySplitting_ThenReturnCamelCase() {
assertThat(toCamelCaseBySplitting("THIS STRING SHOULD BE IN CAMEL CASE", " ")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithUnderscores_WhenToCamelCaseBySplitting_ThenReturnCamelCase() {
assertThat(toCamelCaseBySplitting("THIS_STRING_SHOULD_BE_IN_CAMEL_CASE", "_")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithWhiteSpaces_WhenToCamelCaseBySplittingUsingStreams_ThenReturnCamelCase() {
assertThat(toCamelCaseBySplittingUsingStreams("THIS STRING SHOULD BE IN CAMEL CASE", " ")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithUnderscores_WhenToCamelCaseBySplittingUsingStreams_ThenReturnCamelCase() {
assertThat(toCamelCaseBySplittingUsingStreams("THIS_STRING_SHOULD_BE_IN_CAMEL_CASE", "_")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithWhiteSpaces_WhenToCamelCaseUsingApacheCommonsText_ThenReturnCamelCase() {
assertThat(CaseUtils.toCamelCase("THIS STRING SHOULD BE IN CAMEL CASE", false, ' '))
.isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithUnderscores_WhenToCamelCaseUsingApacheCommonsText_ThenReturnCamelCase() {
assertThat(CaseUtils.toCamelCase("THIS_STRING_SHOULD_BE_IN_CAMEL_CASE", false, '_'))
.isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithWhiteSpaces_WhenToCamelCaseUsingICU4J_ThenReturnCamelCase() {
assertThat(toCamelCaseUsingICU4J("THIS STRING SHOULD BE IN CAMEL CASE", " ")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithUnderscores_WhenToCamelCaseUsingICU4J_ThenReturnCamelCase() {
assertThat(toCamelCaseUsingICU4J("THIS_STRING_SHOULD_BE_IN_CAMEL_CASE", "_")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithWhiteSpaces_WhenToCamelCaseUsingGuava_ThenReturnCamelCase() {
assertThat(toCamelCaseUsingGuava("THIS STRING SHOULD BE IN CAMEL CASE", " ")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithUnderscores_WhenToCamelCaseUsingGuava_ThenReturnCamelCase() {
assertThat(toCamelCaseUsingGuava("THIS_STRING_SHOULD_BE_IN_CAMEL_CASE", "_")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithWhiteSpaces_WhenToCamelCaseUsingApacheCommons_ThenReturnCamelCase() {
assertThat(toCamelCaseUsingApacheCommons("THIS STRING SHOULD BE IN CAMEL CASE", ' ')).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithUnderscores_WhenToCamelCaseUsingApacheCommons_ThenReturnCamelCase() {
assertThat(toCamelCaseUsingApacheCommons("THIS_STRING_SHOULD_BE_IN_CAMEL_CASE", '_')).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithWhiteSpaces_WhenToCamelCaseByRegex_ThenReturnCamelCase() {
assertThat(toCamelCaseByRegex("THIS STRING SHOULD BE IN CAMEL CASE")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenStringWithWhiteUnderscores_WhenToCamelCaseByRegex_ThenReturnCamelCase() {
assertThat(toCamelCaseByRegex("THIS_STRING_SHOULD_BE_IN_CAMEL_CASE")).isEqualTo("thisStringShouldBeInCamelCase");
}
@Test
public void givenRandomString_WhenToCamelCaseByRegex_ThenReturnCamelCase() {
assertThat(toCamelCaseByRegex("Please Turn this56738 to camel Case")).isEqualTo("pleaseTurnThis56738ToCamelCase");
}
@Test
public void givenRandomStringWithDifferentDelimiters_WhenToCamelCaseByRegex_ThenReturnCamelCase() {
assertThat(toCamelCaseByRegex("Please Turn this56738 to camel Case This should-be_in;camel-case")).isEqualTo("pleaseTurnThis56738ToCamelCaseThisShouldBeInCamelCase");
}
@Test
public void givenUppercaseWordWithUnderscores_WhenCaseFormatToLowerCamel_ThenReturnCamelCase() {
assertThat(CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "THIS_STRING_SHOULD_BE_IN_CAMEL_CASE")).isEqualTo("thisStringShouldBeInCamelCase");
}
}