Java program to convert first character uppercase in a sentence



In this article, we will learn to convert the first character to uppercase in a sentence in Java, converting the first character of each word or sentence to uppercase is a common text manipulation task. It ensures proper formatting, which is particularly useful in text processing, string parsing, and user interface applications.

Approaches to convert first character uppercase in a sentence

Following are the two approaches to converting the first character of a sentence to uppercase ?

Using String Manipulation and Character Methods

This approach involves extracting the first character of the sentence, converting it to uppercase using Java's Character.toUpperCase() method, and then appending the rest of the sentence.

  • Character.toUpperCase(): This method in Java converts a given character to its uppercase equivalent. If the character is already uppercase or not alphabetic, it remains unchanged.
  • charAt(): This method returns the character at a specified index in a string. The index is zero-based, where the first character is at index 0.

Convert the first character to uppercase ?

String result = Character.toUpperCase(sentence.charAt(0)) + sentence.substring(1);

Example

Below is an example of converting the first character to uppercase in a sentence using string manipulation and character methods ?

public class ConvertFirstChar {
    public static void main(String[] args) {
        String sentence = "hello world! welcome to java programming.";
        
        // Check for empty or null input
        if (sentence == null || sentence.isEmpty()) {
            System.out.println("Invalid input");
            return;
        }
        
        // Convert first character to uppercase
        String result = Character.toUpperCase(sentence.charAt(0)) + sentence.substring(1);
        
        // Print the result
        System.out.println("Converted Sentence: " + result);
    }
}

Output

Converted Sentence: Hello world! welcome to java programming

Using Regular Expressions with the Matcher Method

In this approach, we use regular expressions to replace the first character of the sentence with its uppercase equivalent. This is achieved using the matcher.replaceFirst() method combined with a regex pattern.

  • ^(.): The regular expression ^ matches the beginning of the string, and (.) captures the first character as a group.
  • Replacement: The replaceFirst method replaces the first match with the uppercase version of the captured group (matcher.group(1).toUpperCase()).
  • Matcher Usage: The Matcher object applies the regex to the input string.

Use regex to capitalize the first character ?

String result = matcher.replaceFirst(matcher.group(1).toUpperCase());

Example

Below is an example of converting the first character to uppercase in a sentence using regular expressions with the matcher method ?

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

public class Main {
    public static void main(String[] args) {
        String sentence = "hello world! welcome to java programming.";
        
        // Use regex to capitalize the first character
        Pattern pattern = Pattern.compile("^(.)");
        Matcher matcher = pattern.matcher(sentence);
        
        if (matcher.find()) {
            String result = matcher.replaceFirst(matcher.group(1).toUpperCase());
            
            // Print the result
            System.out.println("Converted Sentence: " + result);
        } else {
            System.out.println("No match found.");
        }
    }
}

Output

Converted Sentence: Hello world! welcome to java programming

Comparison Table

Aspect String Manipulation Regular Expressions
Readability Simple and straightforward Slightly more complex due to the regex
Performance Faster for small strings Slight overhead with regex matching
Use Case Best for simple sentence manipulations Useful for more complex string patterns
Method Used charAt(), toUpperCase(), substring matcher.replaceFirst() with a regex pattern
Alshifa Hasnain
Alshifa Hasnain

Converting Code to Clarity

Updated on: 2024-12-31T22:01:37+05:30

665 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements