String toUpperCase method is used to converts all characters in a given string to upper case. String has 2 toUpperCase methods.
1 2 3 |
public String toUpperCase() public String toUpperCase(Locale locale) |
The first method takes a default Locale for converting the String to upper case, while in the second method we can explicitly give a Locale for the String to convert to upper case.
Lets see an example that covers both these methods.
String toUpperCase example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.kscodes.sampleproject; import java.util.Locale; public class StringToUpperCaseExample { public static void main(String[] args) { String testStr1 = "This is a test String that will be converted to uppercase"; System.out.println("Using toUpperCase - Default Locale to convert :: "); System.out.println(testStr1.toUpperCase()); System.out.println("----------------------------------------"); System.out.println("Using toUpperCase(Locale locale) :: "); System.out.println(testStr1.toUpperCase(Locale.ITALIAN)); } } |
Output
1 2 3 4 5 |
Using toUpperCase - Default Locale to convert :: THIS IS A TEST STRING THAT WILL BE CONVERTED TO UPPERCASE ---------------------------------------- Using toUpperCase(Locale locale) :: THIS IS A TEST STRING THAT WILL BE CONVERTED TO UPPERCASE |
Please Note:
Before performing any operation on any dynamically generated String, check if the String is not null.
Else a NullPointerException is thrown.
Before performing any operation on any dynamically generated String, check if the String is not null.
Else a NullPointerException is thrown.