From b8481873e9741827e5cfe1bd3b7eafaf937a0a22 Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Thu, 20 Oct 2022 03:31:27 +0200 Subject: [PATCH] [Capitalize_1st] Capitalize the First Letter of a String in Java (#12885) --- .../core-java-string-operations-5/pom.xml | 12 ++++- .../CapitalizeTheFirstLetterUnitTest.java | 45 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/capitalizethefirstletter/CapitalizeTheFirstLetterUnitTest.java diff --git a/core-java-modules/core-java-string-operations-5/pom.xml b/core-java-modules/core-java-string-operations-5/pom.xml index a0c630cb0f..3f78b8d5d3 100644 --- a/core-java-modules/core-java-string-operations-5/pom.xml +++ b/core-java-modules/core-java-string-operations-5/pom.xml @@ -1,7 +1,7 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 core-java-string-operations-5 0.1.0-SNAPSHOT @@ -14,6 +14,13 @@ 0.0.1-SNAPSHOT + + + org.apache.commons + commons-lang3 + ${apache.commons.lang3.version} + + @@ -30,6 +37,7 @@ 11 11 + 3.12.0 \ No newline at end of file diff --git a/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/capitalizethefirstletter/CapitalizeTheFirstLetterUnitTest.java b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/capitalizethefirstletter/CapitalizeTheFirstLetterUnitTest.java new file mode 100644 index 0000000000..865fddd08a --- /dev/null +++ b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/capitalizethefirstletter/CapitalizeTheFirstLetterUnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.capitalizethefirstletter; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; + +import java.util.regex.Pattern; + +import static org.junit.jupiter.api.Assertions.*; + +public class CapitalizeTheFirstLetterUnitTest { + private static final String EMPTY_INPUT = ""; + private static final String EMPTY_EXPECTED = ""; + private static final String INPUT = "hi there, Nice to Meet You!"; + private static final String EXPECTED = "Hi there, Nice to Meet You!"; + + @Test + void givenString_whenCapitalizeUsingSubString_shouldGetExpectedResult() { + String output = INPUT.substring(0, 1).toUpperCase() + INPUT.substring(1); + assertEquals(EXPECTED, output); + + assertThrows(IndexOutOfBoundsException.class, () -> EMPTY_INPUT.substring(1)); + + } + + @Test + void givenString_whenCapitalizeUsingRegexReplace_shouldGetExpectedResult() { + String output = Pattern.compile("^.").matcher(INPUT).replaceFirst(m -> m.group().toUpperCase()); + assertEquals(EXPECTED, output); + + String emptyOutput = Pattern.compile("^.").matcher(EMPTY_INPUT).replaceFirst(m -> m.group().toUpperCase()); + assertEquals(EMPTY_EXPECTED, emptyOutput); + } + + @Test + void givenString_whenCapitalizeUsingApacheCommons_shouldGetExpectedResult() { + String output = StringUtils.capitalize(INPUT); + assertEquals(EXPECTED, output); + + String emptyOutput = StringUtils.capitalize(EMPTY_INPUT); + assertEquals(EMPTY_EXPECTED, emptyOutput); + + String nullOutput = StringUtils.capitalize(null); + assertNull(nullOutput); + } +}