StringUtils capitalize() example in Java

In this post, we will see org.apache.commons.lang3.StringUtils capitalize() example in Java. Consider we have a String variable, and we want to capitalize the first letter of the String then we can use capitalize() method.

StringUtils capitalize() example in Java

import org.apache.commons.lang3.StringUtils;

public class CapitalizeExample {

    public static void main(String[] args) {
        String str = "hello world";
        String capitalizedStr = StringUtils.capitalize(str);
        
        System.out.println(capitalizedStr); // Output: Hello world
    }
}

We need to add the below dependency in pom.xml to use org.apache.commons.lang3.StringUtils capitalize() method. We can download apache-commons maven dependency from here.

pom.xml changes

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

Another capitalize() example in Java

package com.manish.study;

import org.apache.commons.lang3.StringUtils;

import java.beans.*;

public class StringProgramExample {
    public static void main(String[] args) {
        {
            String string = "banana";
            System.out.println("String: " + string);
            StringUtils StringUtils;
            System.out.println("StringUtils.capitalize(string): " + org.apache.commons.lang3.StringUtils.capitalize(string));

            string = "aPRICOT";
            System.out.println("String: " + string);
            System.out.println("StringUtils.capitalize(string): " + org.apache.commons.lang3.StringUtils.capitalize(string));

            string = "PIne apple";
            System.out.println("String: " + string);
            System.out.println("StringUtils.capitalize(string): " + org.apache.commons.lang3.StringUtils.capitalize(string));

            string = "tOmato";
            System.out.println("String: " + string);
            System.out.println("StringUtils.capitalize(string): " + org.apache.commons.lang3.StringUtils.capitalize(string));
        }
    }
}

Output is

String: banana
StringUtils.capitalize(string): Banana
String: aPRICOT
StringUtils.capitalize(string): APRICOT
String: PIne apple
StringUtils.capitalize(string): PIne apple
String: tOmato
StringUtils.capitalize(string): TOmato

That’s all about StringUtils capitalize() example in Java.

Related post.