Java String toUpperCase: The Complete Guide
The toUpperCase() method has two variants. The first variant converts all of the characters in this String to the upper case using the given Locale rules. This is equivalent to calling toUpperCase(Locale.getDefault()).
Java String toUpperCase()
Java String toUpperCase() is a built-in function used to convert any string to all lower characters. Java string toUpperCase() method returns the String in the uppercase letter. In other words, it converts all characters of the String into an upper case letter.
Variants
The two variants are:
String toUpperCase():
This method gets the current value of the default locale using the toUpperCase (Locale.getDefault()) method for the instance of the Java Virtual Machine. This Java Virtual Machine sets the default locale during the start of the host environment. However, it can be changed using the setDefault() method.
Syntax:
public String toUpperCase()
See the following program.
class Upper { public static void main(String args[]) { // initializing the string String s = "Welcome to APPDIVIDEND"; // converting string s to uppercase letter String str = s.toUpperCase(); System.out.println(str); } }
See the output.
String toUpperCase(Locale locale)
The method converts a string into UpperCase using the rules defined by some specified Locale.
Syntax:
public String toUpperCase(Locale loc)
Here loc converts all characters into uppercase values using the rules defined in Locale.
Example
See the following program.
import java.util.Locale; class Upper { public static void main(String args[]) { // initializing the string String s = "Welcome to appdividenD."; // converting string s to uppercase letter using Locale method String eng = s.toUpperCase(Locale.ENGLISH); System.out.println(eng); String turkish = s.toUpperCase(Locale.forLanguageTag("tr")); // It shows i without dot System.out.println(turkish); } }
See the output.
Java toUpperCase(Locale locale)
See the following code example.
import java.util.Locale; public class StringUpper { public static void main(String[] args) { String s = "Breaking Bad"; String turkish = s.toUpperCase(Locale.forLanguageTag("tr")); String english = s.toUpperCase(Locale.forLanguageTag("en")); System.out.println(turkish);// will print I with dot on upper side System.out.println(english); } }
See the output.
➜ java javac StringUpper.java ➜ java java StringUpper BREAKİNG BAD BREAKING BAD ➜ java
That’s it for this tutorial.