这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}