String toUpperCase() method in java

Let’s learn string toUpperCase() method in java.

String toUpperCase() method in java

String toUpperCase method have two variants.

  • public String toUpperCase()
  • public String toUpperCase(Locale locale)

String toUpperCase() method converts a string to uppercase letters.

Also read – how to sort a string array in java

Here’s String toUpperCase() method example or java program to convert string into uppercase.

public class StringToUppercaseDemo
{
   public static void main(String[] args)
   {
      String str = "flower brackets";
      System.out.println(str.toUpperCase());
   }
}


Output:

FLOWER BRACKETS


Whereas touppercase(Locale loc) converts all the characters to uppercase using the rules of the given Locale.

public String toUpperCase(Locale locale)

Also read – java string methods

Now let’s see java String toUpperCase(Locale loc) example.

import java.util.Locale;
public class ToUppercaseLocaleDemo
{
   public static void main(String[] args)
   {
      String str = "i love you java";
      Locale turkish = Locale.forLanguageTag("tr");
      Locale english = Locale.forLanguageTag("en");
      String str1 = str.toUpperCase(turkish);
      String str2 = str.toUpperCase(english);
      System.out.println(str1);
      System.out.println(str2);
   }
}


Output:

? LOVE YOU JAVA
I LOVE YOU JAVA


how to convert first letter of string to uppercase in java

Let’s learn how to convert first letter of string to uppercase in java.

Here’s the simple way to capitalize the first letter of a string in java using String. substring() method.

public class FirstLetterToUpperCase
{
   public static void main(String[] args) 
   {
      String strGiven = "flower brackets";
      String strOutput = strGiven.substring(0, 1).toUpperCase() + strGiven.substring(1);
      System.out.println("Given string: " + strGiven);
      System.out.println("Output - first letter to uppercase: " + strOutput);
   }
}


Output:

Given string: flower brackets
Output – first letter to uppercase: Flower brackets


touppercase java char

java.lang.Character.toUpperCase() method converts the character argument to uppercase using case mapping information from the Unicode Data file.

java.lang.Character.toUpperCase(char ch) method returns the uppercase equivalent of the character, if any; otherwise, the character itself.

Let’s see java.lang.Character.toUpperCase(char ch) method example.

public class ToUpperCaseChar 
{
   public static void main(String[] args) 
   {
      char ch1, ch2, ch3, ch4;
      ch1 = 'r';
      ch2 = 'g';
      ch3 = Character.toUpperCase(ch1);
      ch4 = Character.toUpperCase(ch2);
      String strOne = "Uppercase of " + ch1 + " is " + ch3;
      String strTwo = "Uppercase of " + ch2 + " is " + ch4;
      System.out.println(strOne);
      System.out.println(strTwo);
   }
}


Output:

Uppercase of r is R
Uppercase of g is G


program to convert lowercase to uppercase in java without using string function

Let’s learn program to convert lowercase to uppercase in java without using (any library function) string function.

To convert lowercase to uppercase subtract 32 from lowercase alphabet’s ascii code to convert to uppercase. Here’s program to convert lowercase to uppercase.

public class WithoutStringFunction
{
   static String toUpperMethod(String strInput) 
   {
      String strOutput = "";
      char ch = ' ';
      for(int a = 0; a < strInput.length(); a++)
      {
         // here we are checking for valid alphabet and in lower case
         if(strInput.charAt(a) >= 'a' && strInput.charAt(a) <= 'z') 
         {
            ch = (char)(strInput.charAt(a) - 32);
         }
         else
         {
            ch = (char)(strInput.charAt(a));
         }
         strOutput += ch;
      }
      return strOutput;
   }
   public static void main(String[] args) 
   {
      System.out.println(toUpperMethod("369fkld#3258"));
      System.out.println(toUpperMethod("HELLO$3239"));
      System.out.println(toUpperMethod("flower brackets"));
   }
}


Output:

369FKLD#3258
HELLO$3239
FLOWER BRACKETS