Java - String toUpperCase() Method



The Java String toUpperCase() method is used to convert all the characters of the given string into uppercase letters or alphabets.

This method has two polymorphic variants, one without any arguments and the other one uses the criteria outlined by the given Locale data type to convert the string into uppercase. The syntaxes of these are given below.

Note − Keep in mind that case mapping is based on the Unicode Standard Version of Character class standard. Since the case mappings are not necessarily 1:1, the length of the new string that is produced may or may not match that of the original String.

Syntax

Following is the syntax for Java String toUpperCase() method −

public String toUpperCase()
or,
public String toUpperCase(Locale locale)

Parameters

  • locale − use the case transformation rules for this locale.

Return Value

This method returns the string converted to uppercase.

Example

The following example shows the usage of Java String toUpperCase() method by converting the given characters of the string into its upper case letters without passing any arguments −

import java.lang.*; 
public class StringDemo {
   public static void main(String[] args) {
      
      // converts all lower case letters in to upper case letters
      String str1 = "This is TutorialsPoint";
      System.out.println("string value = " + str1.toUpperCase());    
      str1 = "www.tutorialspoint.com";
      System.out.println("string value = " + str1.toUpperCase());
   }
}

Output

If you compile and run the program above, the output will be displayed as follows −

string value = THIS IS TUTORIALSPOINT
string value = WWW.TUTORIALSPOINT.COM

Example

Below is an example to convert the characters in a string to upper case by passing a Locale argument using toUpperCase() method −

import java.lang.*;
import java.util.*; 
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "This is TutorialsPoint";   
      
      // using the default system Locale
      Locale defloc = Locale.getDefault();        
      
      // converts all lower case letters in to upper case letters
      System.out.println("string value = " + str1.toUpperCase(defloc));    
      str1 = "www.tutorialspoint.com";
      System.out.println("string value = " + str1.toUpperCase(defloc));
   }
}

Output

If you compile and run the above program, it will produce the following result −

string value = THIS IS TUTORIALSPOINT
string value = WWW.TUTORIALSPOINT.COM

Example

Let's create another code that will generate a string of characters that includes letters, numerals, and symbols. In this program, we will determine whether or not the toUpperCase() method affects non-alphabetical characters like numbers and symbols −

public class StringDemo {
   public static void main(String[] args) {
      String s = "Welcome to @!! Tutorials point 77!!";
      System.out.println("The given string  is: " + s);
      String toUpper = s.toUpperCase();
      System.out.println("String after conversion is: " + toUpper);
   }
}

Output

On executing the program above, the output is obtained as follows −

The given string  is: Welcome to @!! Tutorials point 77!!
String after conversion is: WELCOME TO @!! TUTORIALS POINT 77!!

Example

In the example given below we are creating a program that converts the string “Welcome to turialspoint” into uppercase letters by passing the Locale object as an argument to this method −

import java.util.Locale;
public class StringDemo {
   public static void main(String[] args) {
      String s = new String("Welcome to turialspoint");
      System.out.println("The given string is: " + s);
      
      // Create Locales with language "Eng" for English.
      Locale English = Locale.forLanguageTag("Eng");
      System.out.println("Uppercase letters in english: " + s.toUpperCase(English));
   }
}

Output

The output of the above code is as shown below −

The given string is: Welcome to turialspoint
Uppercase letters in turkish: WELCOME TO TURIALSPOINT
Uppercase letters in english: WELCOME TO TURIALSPOINT
java_lang_string.htm
Advertisements