How to Capitalize the First Letter of a String in Java

Payel Ganguly Feb 02, 2024
  1. Capitalize the First Letter of a String Using upperCaseFirst() Associated With toCharArray() Method
  2. Capitalize the First Letter of a String Using toUpperCase() and appendTail() Methods
  3. Capitalize the First Letter of a String Using String.substring()
  4. Capitalize the First Letter of a String Using String.substring() Method With Function capitalize()
How to Capitalize the First Letter of a String in Java

This tutorial article will introduce how to capitalize the first letter of a string using Java. There are some common methods which are used to convert the first letter of a given string value to upper case. The different methods are upperCaseFirst() along with toCharArray(), toUpperCase() and appendTail() methods, String.substring() method and capitalize() function along with String.substring() method. Let us discuss each method implementations through examples.

Capitalize the First Letter of a String Using upperCaseFirst() Associated With toCharArray() Method

In this process, we introduce the upperCaseFirst() method that receives a string value and converts it into an array of characters. Then, we use the Character class and toUpperCase() method to capitalize the first element in the character array. In conclusion, we convert the updated character array into a string using the String Constructor. Let us follow the below example.

import java.io.*;
import java.lang.*;
import java.util.*;

public class Main {
  public static String upperCaseFirst(String val) {
    char[] arr = val.toCharArray();
    arr[0] = Character.toUpperCase(arr[0]);
    return new String(arr);
  }

  public static void main(String[] args) {
    String val1 = "java";
    String val2 = "advanced java";

    String output = upperCaseFirst(val1);
    System.out.println(val1);
    System.out.println(output);

    output = upperCaseFirst(val2);
    System.out.println(val2);
    System.out.println(output);
  }
}

Output:

java
Java
advanced java
Advanced java

Capitalize the First Letter of a String Using toUpperCase() and appendTail() Methods

In the way out, 2 different methods come into picture which are toUpperCase() and appendTail(). For implementing these 2 methods within a single application, regex.Matcher and regex.Pattern packages are imported. The below example will explain these in detail.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] args) {
    String str = "hello world!";
    System.out.println(str);
    StringBuffer strbf = new StringBuffer();
    Matcher match = Pattern.compile("([a-z])([a-z]*)", Pattern.CASE_INSENSITIVE).matcher(str);
    while (match.find()) {
      match.appendReplacement(strbf, match.group(1).toUpperCase() + match.group(2).toLowerCase());
    }
    System.out.println(match.appendTail(strbf).toString());
  }
}

Output:

hello world!
Hello World!

Capitalize the First Letter of a String Using String.substring()

The simplest and easiest trick to capitalize the first letter of a given string is using the String.substring() method. Let us discuss in the below example.

import java.util.*;

public class Main {
  public static void main(String[] args) {
    String str = "java";
    String firstLtr = str.substring(0, 1);
    String restLtrs = str.substring(1, str.length());

    firstLtr = firstLtr.toUpperCase();
    str = firstLtr + restLtrs;
    System.out.println("The modified string is: " + str);
  }
}

Output:

The modified string is: Java

In the above example, we created one string variable - str. Then we formed two substrings from str, where the firstLtr represents the first letter of the string and the restLtrs represent the remaining letters of the string. In the concluding part, we converted the firstLtr to upper case using the toUpperCase() method and joined the two substrings forming the string itself.

Capitalize the First Letter of a String Using String.substring() Method With Function capitalize()

In this last example, we will use a functional capitalize() to ensure that the given string has at least one character before using the String.substring() method.

import java.util.*;

public class Main {
  public static String capitalize(String str) {
    if (str == null || str.isEmpty()) {
      return str;
    }
    return str.substring(0, 1).toUpperCase() + str.substring(1);
  }

  public static void main(String[] args) {
    String str = "hello world!";
    String firstLtr = str.substring(0, 1);
    String restLtrs = str.substring(1, str.length());

    firstLtr = firstLtr.toUpperCase();
    str = firstLtr + restLtrs;
    System.out.println("The modified string is: " + str);
  }
}

Output:

The modified string is: Hello world!

Related Article - Java String