String toUpperCase() method in java

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

String toUpperCase() method in java

String toUpperCase() method converts all characters in given string to uppercase using the rules of default locale.

Well, converting input string to uppercase is very straightforward.

Also read – how to sort a string array in java

String touppercase java have two different methods. Here’s the syntax,

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

public String toUpperCase()

java toUppercase() method converts all of the characters in this String to uppercase using the rules of the default locale.

toUppercase() method returns string in uppercase letter.

Let’s see String toUpperCase() method example and java program to convert string into uppercase,

public class StringToUppercaseDemo 
{
   public static void main(String[] args) 
   { 
      String strConvert = "flower brackets"; 
      String toUpper = strConvert.toUpperCase(); 
      System.out.println("To Uppercase - " + toUpper);
   }
}



Output:

string toUpperCase() method in java

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

public String toUpperCase(Locale locale)

String toUpperCase(Locale locale) method converts all of the characters in this String to uppercase using the rules of the given Locale.

String java.lang.String.toUpperCase(Locale locale) returns the String, converted to uppercase.

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 to uppercase

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 StringToUppercaseDemo 
{
   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

The 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 = '*';
      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 * is *

program to convert lowercase to uppercase without using function

Let’s learn program to convert lowercase to uppercase in java without using 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 ToUpperExample 
{
   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