Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

The Java standard library has provided the String.toUpperCase() method, which allows us to convert all letters in a string to upper case.

In this tutorial, we’ll learn how to convert a given string’s first character only to upper case.

2. Introduction to the Problem

An example can explain this problem quickly. Let’s say we have an input string:

String INPUT = "hi there, Nice to Meet You!";

Given this INPUT string, here’s our expected result:

String EXPECTED = "Hi there, Nice to Meet You!";

As we can see, we only want the first character, ‘h‘, to be changed into ‘H‘. However, the remaining characters shouldn’t be modified.

Of course, if the input string is empty, the result should be an empty string too:

String EMPTY_INPUT = "";
String EMPTY_EXPECTED = "";

In this tutorial, we’ll address several solutions to the problem. For simplicity, we’ll use unit test assertions to verify if our solution works as expected.

3. Using the substring() Method

The first idea to solve the problem is to split the input string into two substrings. For example, we can split the INPUT string to “h” and “i there, Nice ….“. In other words, the first substring contains only the first character, and the other substring holds the remaining characters of the strings.

Then, we can just apply the toUpperCase() method on the first substring and concatenate the second substring to solve the problem.

Java’s String class’s substring() method can help us to get the two substrings:

  • INPUT.substring(0, 1) – substring 1 containing the first character
  • INPUT.substring(1) – substring 2 holding the rest of the characters

So next, let’s write a test to see if the solution works:

String output = INPUT.substring(0, 1).toUpperCase() + INPUT.substring(1);
assertEquals(EXPECTED, output);

If we run the test, it passes. However, if our input is an empty string, this approach will raise IndexOutOfBoundsException. This is because the end-index (1) is greater than the empty string’s length (0) when we call INPUT.substring(1):

assertThrows(IndexOutOfBoundsException.class, () -> EMPTY_INPUT.substring(1));

Further, we should note that if the input string is null, this approach will throw NullPointerException.

Therefore, before using the substring approach, we need to check and ensure the input string is not null or empty.

4. Using the Matcher.replaceAll() Method

Another idea to solve the problem is to use regex (“^.“) to match the first character and convert the matched group to upper case.

It wasn’t an easy task before Java 9. This is because Matcher‘s replacement methods, such as replaceAll() and replaceFirst(), don’t support a Function object or a lambda expression replacer. However, this has changed in Java 9.

Since Java 9, Matcher‘s replacement methods support a Function object as the replacer. That is to say, we can use a function to process the matched character sequence and fulfill the replacement. Of course, to solve our problem, we just need to call the toUpperCase() method on the matched character:

String output = Pattern.compile("^.").matcher(INPUT).replaceFirst(m -> m.group().toUpperCase());
assertEquals(EXPECTED, output);

The test passes if we give it a run.

If the regex matches nothing, the replacement won’t happen. Therefore, this solution works for empty input strings as well:

String emptyOutput = Pattern.compile("^.").matcher(EMPTY_INPUT).replaceFirst(m -> m.group().toUpperCase());
assertEquals(EMPTY_EXPECTED, emptyOutput);

It’s worth mentioning that if the input string is null, this solution will throw NullPointerException too. So, we still need to do a null check before we use it.

5. Using StringUtils From Apache Commons Lang 3

Apache Commons Lang3 is a popular library. It ships with a lot of handy utility classes and extends the functionality of the standard Java library.

Its StringUtils class provides the capitalize() method, which solves our problem directly.

To use the library, let’s first add the Maven dependency:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

Then, as usual, let’s create a test to see how it works:

String output = StringUtils.capitalize(INPUT);
assertEquals(EXPECTED, output);

The test passes if we execute it. As we can see, we simply call StringUtils.capitalize(INPUT). Then the library does the job for us.

It’s worth mentioning that the StringUtils.capitalize() method is null-safe and works for empty input strings as well:

String emptyOutput = StringUtils.capitalize(EMPTY_INPUT);
assertEquals(EMPTY_EXPECTED, emptyOutput);
String nullOutput = StringUtils.capitalize(null);
assertNull(nullOutput);

6. Conclusion

In this article, we’ve learned how to convert the first character of a given string to upper case.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW