Java String toUpperCase()

The String.toUpperCase() in Java converts all the characters in a string to UPPERCASE. The previous uppercases characters and non-alphabetic characters remain unchanged.

To make a string all lowercase, use toLowerCase() method.

1. String.toUpperCase() API

The toUpperCase() takes an optional argument of type Locale and returns a new String after converting all the alphabetic characters to uppercase according to the specified Locale rules.

String upperCased = "Alex123".toUpperCase();

String upperCased = "Alex123".toUpperCase(Locale.US);

The toUppercase() method is equal to calling the toUpperCase(Locale.getDefault()) method that uses the current Locale rules.

2. String.toUpperCase() with Default Locale

The following Java program converts the specified string to uppercase using default locale rules.

String string = "hello world";

String uppercaseString = string.toUpperCase();

System.out.println(uppercaseString);

Program output.

HELLO WORLD

3. Uppercase with Custom Locale

The following Java program converts a string to uppercase using the Locale.US.

String uppercaseString = "Γειά σου Κόσμε".toUpperCase(Locale.US);

System.out.println(uppercaseString);

Program output.

ΓΕΙΆ ΣΟΥ ΚΌΣΜΕ

Happy Learning !!

References: String Java Doc

Comments

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.