Core Java

Java Capitalize First Letter Example

In this post, we feature a comprehensive Java Capitalize First Letter Example. We want to write a Java program to convert uppercase first letter java in a sentence and if any character apart from the first one is in Uppercase then convert in into Lowercase.

1. Introduction

Firstly, lets see how to capitalize the first letter of a single word, Example01:

String output = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();
public class CaptWord {

    public static void main(String args[]) {

        String input = "wOrD";

        String output = input.substring(0, 1).toUpperCase() + input.substring(1).toLowerCase();

Now we want to capitalize eache word in a sentence:

Input : jAVA
Output :Java

Input :CODE
Output :Code

2. Java Capitalize First Letter – Different Methods

2.1 Method 1

We conver the given string to a char array and conver the first character of each word into upper-case and the rest of its charachters convert into lower-case. See Example02:
public class Capitalize {

    static String convert(String str)

        // Create a char array of given String
        char ch[] = str.toCharArray();
        for (int i = 0; i = 'a' && ch[i] = 'A' && ch[i] <= 'Z')

                // Convert into Lower-Case
                ch[i] = (char)(ch[i] + 'a' - 'A');

        // Convert the char array to equivalent String
        String st = new String(ch);
        return st;

    public static void main(String[] args)
        String str = "jAVA cOdE GeeKs";

Java Code Geeks

2.2 Alternative implementations

2.2.1 Example03

Another way of implementing the previous example is shown in Example03:
public class Capitalize2 {

    public static String upperCaseAllFirst(String value) {

        char[] array = value.toCharArray();

        // Uppercase first letter.
        array[0] = Character.toUpperCase(array[0]);

        // Uppercase all letters that follow a whitespace character.
        for (int i = 1; i < array.length; i++) {
            if (Character.isWhitespace(array[i - 1])) {
                array[i] = Character.toUpperCase(array[i]);

        return new String(array);

    public static void main(String[] args) {

        String value = "phone 00 123456789";
        String value2 = "name is john";

        // Test our code.
        String result = upperCaseAllFirst(value);
        System.out.println("value 1: " + value);
        System.out.println("result 1: " + result);

        result = upperCaseAllFirst(value2);
        System.out.println("value 2: " + value2);
        System.out.println("result 2: " + result);

value 1: phone 00 123456789
result 1: Phone 00 123456789
value 2: name is john
result 2: Name Is John

2.2.2 Example04

We can also use split() method to separate the given sentence into separate words, see

public class StringCapital {

    public static void main(String[] args)
        String str = "welcome to string capital java program";
        StringBuilder result = new StringBuilder(str.length());
        String words[] = str.split("\\ ");
        for (int i = 0; i < words.length; i++)
            result.append(Character.toUpperCase(words[i].charAt(0))).append(words[i].substring(1)).append(" ");




Welcome To String Capital Java Program 

2.2.3 Example05

Lets see another way of implementation in

import java.util.Scanner;

public class CaptAnySent {

    public static void main(String[] args){
        Scanner in = new Scanner(;
        System.out.print("Input your Sentence: ");
        String line = in.nextLine();
        String upperCaseLine = "";
        Scanner lineScan = new Scanner(line);
        while(lineScan.hasNext()) {
            String word =;
            upperCaseLine += Character.toUpperCase(word.charAt(0)) + word.substring(1) + " ";
Input your Sentence: java code geeks students
Java Code Geeks Students

2.3 Method 2: Using Java Inbuilt methods toUpperCase()

We can use the toLowerCase() method to convert the string into lowercase format. Iterate the string, if any space found in previous iteration and the current element is not space then it shows that current letter is the starting of the word so call the toUpperCase() method to put the first letter of word in uppercase format and append the string in buffer. Below is the implementation of

public class InbuiltCapt {

    static String capitailize(String str) {
        StringBuffer buffer = new StringBuffer();

        // Declare a character of space
        // To identify that the next character is the starting of a new word
        char ch = ' ';
        for (int i = 0; i < str.length(); i++) {

            if (ch == ' ' && str.charAt(i) != ' ')
            ch = str.charAt(i);

        return buffer.toString().trim();

    public static void main(String args[]) {

        String s1 = "i aM A tEaChEr";

        // Convert that string into lowercase
        s1 = s1.toLowerCase();

        // Call the method to capitalize each word
I Am A Teacher

2.4 Method 3: Using Java Inbuilt methods WordUtils.capitalizeFully(String str)

Inorder to use WordUtils.capitalizeFully(String) we create a Maven project and add this dependency to its pom.xml. See Example07:


Now we can use the method as

import org.apache.commons.lang3.text.WordUtils;

public class CaptFull {

    public static void main(String args[]) {

        String input = "aNOthEr seNTenCe";

Another Sentence

This was an article about how to create an uppercase first letter in Java.

3. Download the Source Code

You can download the full source code of this example here: Java Capitalize First Letter Example

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Firouzeh hejazi

A self-motivated and hard-working developer with more than 4 years of extensive experience in developing software in java platforms. A multi-skilled and problem-solving engineer who has participated in implementing a range of applications in different roles. Possess a MSc in Knowledge Engineering and Decision Science.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
2 years ago

so what will be the code when I type my last name and it shows it as 1st letter uppercase, the thing is I don’t want it to come separately when I enter and I hope it makes sense

Back to top button