diff --git a/core-java-modules/core-java-string-operations-7/src/main/java/com/baeldung/capitalizefirstcharactereachword/CapitalizeFirstCharacterEachWordUtils.java b/core-java-modules/core-java-string-operations-7/src/main/java/com/baeldung/capitalizefirstcharactereachword/CapitalizeFirstCharacterEachWordUtils.java new file mode 100644 index 0000000000..cccc745499 --- /dev/null +++ b/core-java-modules/core-java-string-operations-7/src/main/java/com/baeldung/capitalizefirstcharactereachword/CapitalizeFirstCharacterEachWordUtils.java @@ -0,0 +1,40 @@ +package com.baeldung.capitalizefirstcharactereachword; + +import java.util.Arrays; +import java.util.stream.Collectors; + +import org.apache.commons.lang3.StringUtils; + +class CapitalizeFirstCharacterEachWordUtils { + + static String usingCharacterToUpperCaseMethod(String input) { + if (input == null || input.isEmpty()) { + return null; + } + + return Arrays.stream(input.split("\\s+")) + .map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1)) + .collect(Collectors.joining(" ")); + } + + static String usingStringToUpperCaseMethod(String input) { + if (input == null || input.isEmpty()) { + return null; + } + + return Arrays.stream(input.split("\\s+")) + .map(word -> word.substring(0, 1).toUpperCase() + word.substring(1)) + .collect(Collectors.joining(" ")); + } + + static String usingStringUtilsClass(String input) { + if (input == null || input.isEmpty()) { + return null; + } + + return Arrays.stream(input.split("\\s+")) + .map(StringUtils::capitalize) + .collect(Collectors.joining(" ")); + } + +} diff --git a/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/capitalizefirstcharactereachword/CapitalizeFirstCharacterEachWordUtilsUnitTest.java b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/capitalizefirstcharactereachword/CapitalizeFirstCharacterEachWordUtilsUnitTest.java new file mode 100644 index 0000000000..27c7ddb4c4 --- /dev/null +++ b/core-java-modules/core-java-string-operations-7/src/test/java/com/baeldung/capitalizefirstcharactereachword/CapitalizeFirstCharacterEachWordUtilsUnitTest.java @@ -0,0 +1,38 @@ +package com.baeldung.capitalizefirstcharactereachword; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.apache.commons.text.WordUtils; +import org.junit.jupiter.api.Test; + +class CapitalizeFirstCharacterEachWordUtilsUnitTest { + + @Test + void givenString_whenUsingCharacterToUpperCaseMethod_thenCapitalizeFirstCharacter() { + String input = "hello baeldung visitors"; + + assertEquals("Hello Baeldung Visitors", CapitalizeFirstCharacterEachWordUtils.usingCharacterToUpperCaseMethod(input)); + } + + @Test + void givenString_whenUsingSubstringMethod_thenCapitalizeFirstCharacter() { + String input = "Hi, my name is azhrioun"; + + assertEquals("Hi, My Name Is Azhrioun", CapitalizeFirstCharacterEachWordUtils.usingStringToUpperCaseMethod(input)); + } + + @Test + void givenString_whenUsingStringUtilsClass_thenCapitalizeFirstCharacter() { + String input = "life is short the world is wide"; + + assertEquals("Life Is Short The World Is Wide", CapitalizeFirstCharacterEachWordUtils.usingStringUtilsClass(input)); + } + + @Test + void givenString_whenUsingWordUtilsClass_thenCapitalizeFirstCharacter() { + String input = "smile sunshine is good for your teeth"; + + assertEquals("Smile Sunshine Is Good For Your Teeth", WordUtils.capitalizeFully(input)); + } + +}