Java String toUpperCase() method explained | technical-arbaab

Java string toUpperCase() method explained




It is a predefined function which is present in the Java.lang.String package. It is used to convert the letters of strings into upper case and returns the resulted string, let's see what is the syntax of this method-
  public String toUpperCase() {}  

let's see this function in some of the programming problems-

Example 01 of toUpperCase() function

 public class ToupperCase {  
      public static void main(String[] args)  
      {  
           String s1 = new String("avengers");  
           System.out.println("before conversion the string is: "+s1);  
           s1=s1.toUpperCase();  
           System.out.println("After conversion the string is: "+s1);  
      }  
 }  
Output:
 before conversion the string is: avengers  
 After conversion the string is: AVENGERS  
Or you can directly convert and print at the same line as shown below-
 public class ToupperCase {  
      public static void main(String[] args)  
      {  
           String s1 = new String("avengers");  
           System.out.println("before conversion the string is: "+s1);  
           System.out.println("After conversion the string is: "+s1.toUpperCase());  
      }  
 }  
Output:
 before conversion the string is: avengers  
 After conversion the string is: AVENGERS   

Example 02 of toUpperCase() function

Problem statement- Take a string from the user and ask for permission whether to convert it into uppercase or not and you have to convert only when user enters a specific string which is "technical-arbaab" or you can choose this any according to you.

Source code-

 public class ToLowercase {  
      public static void main(String[] args)throws IOException  
      {  
           BufferedReader b = new BufferedReader(new InputStreamReader(System.in));  
           System.out.println("Enter a string");  
           String s1 = b.readLine();  
           System.out.println("should i convert this into Uppercase or not");  
           String s2 = b.readLine();  
           if(s2.equalsIgnoreCase("yes") && s1.equalsIgnoreCase("technical-arbaab"))  
           {  
                System.out.println(s1.toUpperCase());  
           }  
           else  
           {  
                return;  
           }       
      }  
 }  
Output:
 Enter a string  
 technical-arbaab  
 should i convert this into Uppercase or not  
 yes  
 TECHNICAL-ARBAAB  
This is all for toUpperCase() method.

Imp.note: There is another method similar to this called as-

  •  toUpperCase(Locale locale): converts the characters of a string using the rules of a given locale. If you want to know more about this method let us know in the comment section below.



Comments