Open In App

How to Check if a String Contains Only Lowercase Letters in Java?

A string is a data structure that contains a set of characters. It can be a word or can be a sentence. Also, it can be empty or can have a single letter. A string can contain Uppercase letters, Lowercase letters, or numbers of special characters.

In this article, we will learn how to check if a String contains only lowercase letters in Java.



Approaches to Check Whether a String Contains Only Lowercase Letters

The idea here is very simple. We will go to each letter and check whether it is a lowercase letter or not. We will see two methods for this.

1. By iterating over the String

Code Implementation:




// Java program to check if a String contains only lowercase letters
import java.util.*;
public class LowercaseChecker 
{
        public static boolean containsOnlyLowercase(String str) 
    {
        for (char c : str.toCharArray()) 
        {
            // check if the character is not a lowercase letter
            if (!(c >= 'a' && c <= 'z')) 
            {
                return false;
            }
        }
        // all characters are lowercase letters
        return true;
    }
  
    public static void main(String args[]) 
    {
        // test cases
        String example1 = "lowercaseonly";
        String example2 = "MixedCase123";
          
        // check if each example contains only lowercase letters
        System.out.println("Example 1 contains only lowercase letters: " + containsOnlyLowercase(example1));
        System.out.println("Example 2 contains only lowercase letters: " + containsOnlyLowercase(example2));
    }
}

Output

Example 1 contains only lowercase letters: true
Example 2 contains only lowercase letters: false

Explanation of the above Program:

Complexity of the above method:

Time complexity: O(N)
Space complexity: O(1)

2. Using String.matches function

In this method, all the things will remain same the change here is instead of checking whether the ASCII value of character lies in between ASCII value of a and z we will use inbuilt function is Java called String.matches. Following is the method for the same.

Code Implementation:




// Java program to check if a String contains
// Only lowercase letters using String.matches function
import java.util.*;
  
// Driver Class
public class LowercaseChecker {
    // Method to Check if a String
      // Contains only lowercase letters
    public static boolean containsOnlyLowercase(String str) 
    {
          // True if the string contains
          // Only lowercase letters, otherwise false
        return str.matches("[a-z]+");            
    }
    // Main Method to test the containsOnlyLowercase method.
    public static void main(String args[]) 
    {
        String example1 = "lowercaseonly";
        String example2 = "MixedCase123";
          
        System.out.println("Example 1 contains only lowercase letters: " + containsOnlyLowercase(example1));
        System.out.println("Example 2 contains only lowercase letters: " + containsOnlyLowercase(example2));
    }
}

Output
Example 1 contains only lowercase letters: true
Example 2 contains only lowercase letters: false

Explanation of the above Program:

Complexity of the above method:

Time complexity: O(N)
Space complexity: O(1)


Article Tags :