What is StringUtils.upperCase in Java?

Overview

upperCase() is a staticthe methods in Java that can be called without creating an object of the class. method of the StringUtils that converts a string to uppercase. This method optionally takes a locale. If the locale is unspecified, the default locale of the system is taken. The method returns null if the input string is null.

How to import StringUtils

The definition of StringUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file:


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

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the StringUtils class as follows:


import org.apache.commons.lang3.StringUtils;

Syntax


public static String upperCase(final String str, final Locale locale)

Parameters

  • final String str: The string to be converted into uppercase.
  • final Locale locale: The locale that defines the case transformation rules.

Return value

This method returns an uppercased string.

Overloaded methods

public static String upperCase(final String str)

Code

import org.apache.commons.lang3.StringUtils;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
String s = "hellO-EDUcative";
System.out.printf("The output of StringUtils.upperCase() for the string - '%s' is %s", s, StringUtils.upperCase(s, Locale.ENGLISH));
System.out.println();
s = "";
System.out.printf("The output of StringUtils.upperCase() for the string - '%s' is %s", s, StringUtils.upperCase(s, Locale.ENGLISH));
System.out.println();
s = null;
System.out.printf("The output of StringUtils.upperCase() for the string - '%s' is %s", s, StringUtils.upperCase(s, Locale.ENGLISH));
System.out.println();
}
}

Example 1

  • string = "hellO-EDUcative"
  • locale = english

The method returns HELLO-EDUCATIVE, where the string is converted to uppercase.

Example 2

  • string = ""
  • locale = english

The method returns `` as the input string is empty.

Example 2

  • string = null
  • locale = english

The method returns null as the input string is null.

Output

The output of the code is:


The output of StringUtils.upperCase() for the string - 'hellO-EDUcative' is HELLO-EDUCATIVE
The output of StringUtils.upperCase() for the string - '' is 
The output of StringUtils.upperCase() for the string - 'null' is null
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources

Attributions:
  1. undefined by undefined