Java String toLowerCase() method explained | technical-arbaab

Java String toLowerCase() 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 lower case and returns the resulted string, let's see what is the syntax of this method-

 public String toLowerCase() {}  

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

Example 01 of toLowerCase() function

 public class ToLowercase {  
      public static void main(String[] args)  
      {  
           String s1 = new String("AVENGERS");  
           System.out.println("Before converting String is:"+s1);  
           s1=s1.toLowerCase();  
           System.out.println("After converting String is:"+s1);  
      }  
 }
Output:
 
Before converting String is:AVENGERS  
After converting String is:avengers  

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

Example 02 of toLowerCase() function

Problem statement: Take a string from the user and ask for permission whether to convert it into lowercase 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-

 import java.io.*;  
 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 lowercase or not");  
           String s2 = b.readLine();  
           if(s2.equalsIgnoreCase("yes") && s1.equalsIgnoreCase("technical-arbaab"))  
           {  
                System.out.println(s1.toLowerCase());  
           }  
           else  
           {  
                return;  
           }       
      }  
 }  
Output:
 Enter a string  
 TECHNICAL-ARBAAB  
 should i convert this into lowercase or not  
 yes  
 technical-arbaab  
This is all for toLowerCase() method.

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

  •  toLowerCase(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