How to Convert a String to UpperCase in Java?

In the last tutorial, we have learned how to convert a string into a lowercase in Java. We discussed all the two variants to convert all the characters of a string to uppercase. In this tutorial, we will learn how to convert a string to UpperCase in Java. We can convert all the characters of a string to UpperCase by using the toUpperCase() method of Java String Class. Like, toLowerCase() method the toUpperCase() method also has two variants. In this tutorial, we will discuss all the two variants of the toUpperCase() method.




1. toUpperCase(): This is the first variant of toUpperCase() method of String class. This method converts all the characters of a string to UpperCase. This method is equivalent to toUpperCase(Locale.getDefault). 

Syntax: The syntax of the first variant of the toUpperCase() method is given below: 

public String toUpperCase()


Java program to convert a string to UpperCase

class toUpperCase{
	public static void main(String args[]){
		String str = "String Tutorial BY JavaStudyPoint";
		//converting all the characters of
		//a string into uppercase.
		System.out.println(str.toUpperCase());
	}
}

Output:

STRING TUTORIAL BY JAVASTUDYPOINT


2. toUpperCase(Locale locale): This is the second variant of the Java String toUpperCase() method. This method converts all the characters of a string to UpperCase using to rule of a given locale.

Syntax: The syntax of the second variant of the toUpperCase() method is given below:

public String toUpperCase(Locale locale)



Java String toUpperCase(Locale locale) Method Example


import java.util.Locale;
class toUpperCase{
	public static void main(String args[]){
		String str = "String Tutorial BY JavaStudyPoint";
		
		//Locales with the language "tr"
		//for turkish and "en" for english.
		Locale turkish = Locale.forLanguageTag("tr");
		Locale english = Locale.forLanguageTag("en");
		
		//converting string str into lowercase
		//using turkish and english language.
		String str1 = str.toUpperCase(turkish);
		String str2 = str.toUpperCase(english);
		System.out.println(str1);
		System.out.println(str2);
	}
}

Output:
how to convert a string to uppercase in java
How to Convert a String to UpperCase in Java? How to Convert a String to UpperCase in Java? Reviewed by Prashant Srivastava on January 08, 2020 Rating: 5

No comments:

Powered by Blogger.