Java String toUpperCase() Method

Java String toUpperCase() Method | In this blog, we will see the purpose and work of the String toUpperCase() method. This method is used to convert all the letters of the given string to the upper case. The toUpperCase() method has two variants:-

  1. public String toUpperCase()
  2. public String toUpperCase(Locale locale)

Here,
Parameters: locale is used to represent the Locale object.
Return Type:- string
Returns: String that is converted to upper case.

A locale is a class object which represents a geographical, cultural, and political region. It is an identifier of an object but not the container of the object. It can have the fields like country, language, extension script, and more.

Similar to the toUpperCase() method, the Java string class also contains the toLowerCase() method. Now see the example for String toUpperCase() Java:-

Example-1:-
String = “hai”
After using the method: “HAI”

Example-2:-
String = “WORLD”
After using the method: “WORLD”

Example-3:-
String = “JAVA”
After using the method: “JAVA”

String toUpperCase() Java Example

Now, let us see some examples of String toUpperCase() methods. We will take a string and then call the toUpperCase() method on the given string.

public class Main {
   public static void main(String args[]) {
      String string = "Welcome to knowProgram";

      System.out.print("String: ");
      System.out.println(string.toUpperCase());
   }
}

Output:-

String: WELCOME TO KNOWPROGRAM

We can store the result string in a variable so that it can be used later in the programs. Let us see it through an example:-

public class Main {
   public static void main(String args[]) {
      String string = "Hello, World!";
      String uppercase = string.toUpperCase();
      System.out.println(uppercase);
   }
}

Output:-

HELLO, WORLD!

String toUpperCase(Locale) Java Example

If we want to convert the string based on some locale then the Java string class provides the toUpperCase(Locale locale) method. It converts the given string into uppercase based on the given locale. Let us see some examples of it.

import java.util.Locale;

public class Main {
   public static void main(String[] args) {
      String str = "Happy birthday.";

      String turkish = str.toUpperCase(Locale.forLanguageTag("tr"));
      String english = str.toUpperCase(Locale.forLanguageTag("en"));

      System.out.println(turkish);
      System.out.println(english);
   }
}

Output:-

HAPPY BİRTHDAY.
HAPPY BIRTHDAY.

import java.util.Locale;

public class Main {
   public static void main(String[] args) {
      String str = "ΙΧΘΥΣİ";

      Locale turkish = Locale.forLanguageTag("tr");
      Locale english = Locale.forLanguageTag("en");

      String string1 = str.toLowerCase(turkish);
      String string2 = str.toLowerCase(english);
      System.out.println("String: " + str);
      System.out.println("Turkish: " + string1);
      System.out.println("English: " + string2);
   }
}

Output:-

String: ΙΧΘΥΣİ
Turkish: ιχθυσi
English: ιχθυσi̇

For converting String to uppercase, lowercase, alternative case, sentence case, and title case you can also take help from the online tools.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *