BAEL-5025 Example code for camel case to words (#10964)

This commit is contained in:
ashleyfrieze 2021-07-03 17:43:55 +01:00 committed by GitHub
parent 9930b4c945
commit d1d9ada490
4 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package com.baeldung.regex.camelcasetowords;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Convert a string in camelCase or TitleCase into a list of words
*/
public class CamelCaseToWords {
private static final Pattern WORD_FINDER = Pattern.compile("(([A-Z]?[a-z]+)|([A-Z]))");
/**
* Find the words in mixed case string like ThisIsText or HereIsSomeText
* @param text the text to parse
* @return the list of words to process
*/
public static List<String> findWordsInMixedCase(String text) {
Matcher matcher = WORD_FINDER.matcher(text);
List<String> words = new ArrayList<>();
while (matcher.find()) {
words.add(matcher.group(0));
}
return words;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.regex.camelcasetowords;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Recapitalize {
private static final Set<String> STOP_WORDS = Stream.of("a", "an", "the", "and",
"but", "for", "at", "by", "to", "or")
.collect(Collectors.toSet());
public static String sentenceCase(List<String> words) {
List<String> capitalized = new ArrayList<>();
for (int i = 0; i < words.size(); i++) {
String currentWord = words.get(i);
if (i == 0) {
capitalized.add(capitalizeFirst(currentWord));
} else {
capitalized.add(currentWord.toLowerCase());
}
}
return String.join(" ", capitalized) + ".";
}
public static String capitalizeMyTitle(List<String> words) {
List<String> capitalized = new ArrayList<>();
for (int i = 0; i < words.size(); i++) {
String currentWord = words.get(i);
if (i == 0 || !STOP_WORDS.contains(currentWord.toLowerCase())) {
capitalized.add(capitalizeFirst(currentWord));
} else {
capitalized.add(currentWord.toLowerCase());
}
}
return String.join(" ", capitalized);
}
private static String capitalizeFirst(String word) {
return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.regex.camelcasetowords;
import org.junit.jupiter.api.Test;
import static com.baeldung.regex.camelcasetowords.CamelCaseToWords.findWordsInMixedCase;
import static org.assertj.core.api.Assertions.assertThat;
class CamelCaseToWordsUnitTest {
@Test
void givenPlainStringWithNonLetters_thenFindsWords() {
assertThat(findWordsInMixedCase("some words"))
.containsExactly("some", "words");
}
@Test
void givenWordsInCamelCase_thenFindsWords() {
assertThat(findWordsInMixedCase("thisIsCamelCaseText"))
.containsExactly("this", "Is", "Camel", "Case", "Text");
}
@Test
void givenWordsInTitleCase_thenFindsWords() {
assertThat(findWordsInMixedCase("ThisIsTitleCaseText"))
.containsExactly("This", "Is", "Title", "Case", "Text");
}
@Test
void givenWordsAcrossMultipleTexts_thenFindsWords() {
assertThat(findWordsInMixedCase("ThisIsTitleCaseText --- andSoIsThis"))
.containsExactly("This", "Is", "Title", "Case", "Text", "and", "So", "Is", "This");
}
@Test
void givenCamelCaseHasASingleLetterWord_thenItCanBeSplit() {
assertThat(findWordsInMixedCase("thisHasASingleLetterWord"))
.containsExactly("this", "Has", "A", "Single", "Letter", "Word");
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.regex.camelcasetowords;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static com.baeldung.regex.camelcasetowords.Recapitalize.*;
import static org.assertj.core.api.Assertions.assertThat;
class RecapitalizeUnitTest {
@Test
void givenWords_thenCanComposeSentence() {
assertThat(sentenceCase(Arrays.asList("these", "Words", "Form", "A", "Sentence")))
.isEqualTo("These words form a sentence.");
}
@Test
void givenNonStopWords_thenTitleIsComposed() {
assertThat(capitalizeMyTitle(Arrays.asList("title", "words", "capitalize")))
.isEqualTo("Title Words Capitalize");
}
@Test
void givenStopWords_thenTitleHasThemInLowerCase() {
assertThat(capitalizeMyTitle(Arrays.asList("this", "is", "A", "title", "with", "a", "stop", "word", "or", "two")))
.isEqualTo("This Is a Title With a Stop Word or Two");
}
@Test
void givenStopWordIsFirstWord_thenTitleHasItCapitalized() {
assertThat(capitalizeMyTitle(Arrays.asList("a", "stop", "word", "first")))
.isEqualTo("A Stop Word First");
}
}