Java Check If String Contains Uppercase And Lowercase

Java Check If String Contains Uppercase And Lowercase | Java contains several methods to check if the string contains uppercase and lowercase. The Java string class contains toUpperCase() and toLowerCase() method which is used to convert the given string to the uppercase and lowercase. After converting the string we can compare it with the original string.

Similarly, in Character class isUpperCase() and isLowerCase() are the methods that can be useful to check if the string contains uppercase and lowercase.

Java Check If String Contains Uppercase And Lowercase Using toUpperCase(), toLowerCase() & eqauls()

We will use toUpperCase() & toLowerCase() method to convert the given string to upper & lower case respectively. After conversion, if the resultant string is exactly the same as the original string then we can say it is in uppercase or lowercase. Let us see it through the code.

Java Check If String Contains Uppercase And Lowercase Using toUpperCase() & eqauls()

public class Main {
   public static void main(String[] args) {
      String str = "KNOW PROFRAM";
      if (str.equals(str.toUpperCase())) {
         System.out.println("Uppercase");
      } else {
         System.out.println("Not Uppercase");
      }
   }
}

Output:

Uppercase

Java Check If String Contains Uppercase And Lowercase Using toLowerCase() & eqauls()

public class Main {
   public static void main(String[] args) {
      String str = "know program";
      if (str.equals(str.toLowerCase())) {
         System.out.println("Lowercase");
      } else {
         System.out.println("Not Lowercase");
      }
   }
}

Output:

Lowercase

Java Check If String Contains Uppercase And Lowercase Using Character.isUpperCase() & Character.isLowerCase()

In Character class isUpperCase() and isLowerCase() are the methods that can be useful to check if the string contains uppercase and lowercase. These two methods return boolean type. If the given string is in upper case then Character.isUpperCase() returns true else returns false likewise if the string is in lowercase then character.isLowerCase() returns true else returns false. 

Method Syntax:-
public boolean isUpperCase(char ch)
public boolean isLowerCase(char ch)

  • Parameter:- a character .
  • Returns:- boolean value.

As these methods are of character type we need to convert the given string to the character by using toCharArray(). After that, we can use toUpperCase() and toLowerCase() methods to find whether the string is in uppercase or lowercase.

Code to demonstrate Character.isUpperCase()

public class Main {
   public static void main(String[] args) {
      String string = "KNOWPROGRAM";
      System.out.println(isStringUpperCase(string));
   }

   private static boolean isStringUpperCase(String string) {
      char[] ch = string.toCharArray();
      for (int i = 0; i < ch.length; i++) {
         if (!Character.isUpperCase(ch[i])) {
            return false;
         }
      }
      return true;
   }
}

Output:

true

Code to demonstrate Character.isLowerCase()

public class Main {
   public static void main(String[] args) {
      String string = "knowprogram";
      System.out.println(isStringLowerCase(string));
   }

   private static boolean isStringLowerCase(String string) {
      char[] ch = string.toCharArray();
      for (int i = 0; i < ch.length; i++) {
         if (!Character.isLowerCase(ch[i]))
            return false;
      }
      return true;
   }
}

Output:-

true

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.