Introduction
In this lab, you will learn how to check if a string is in lowercase in Java. We will explore different methods to achieve this, starting with comparing strings while ignoring case using toLowerCase() and equalsIgnoreCase().
You will then learn how to iterate through a string and use Character.isLowerCase() to check each character individually. Finally, we will cover how to handle non-letter characters when performing this check.
Compare String with toLowerCase()
In this step, we will learn how to compare strings in Java while ignoring the case of the letters. This is a common task when you want to check if two strings are the same regardless of whether they use uppercase or lowercase letters.
Let's create a new Java file to practice this.
Open the WebIDE if it's not already open.
In the File Explorer on the left, make sure you are in the
~/projectdirectory.Right-click in the empty space in the File Explorer, select "New File", and name it
CaseInsensitiveCompare.java.Open the
CaseInsensitiveCompare.javafile in the editor.Copy and paste the following code into the editor:
public class CaseInsensitiveCompare { public static void main(String[] args) { String str1 = "Hello"; String str2 = "hello"; String str3 = "World"; // Comparing strings directly (case-sensitive) boolean areEqualCaseSensitive = str1.equals(str2); System.out.println("Case-sensitive comparison of \"" + str1 + "\" and \"" + str2 + "\": " + areEqualCaseSensitive); // Comparing strings ignoring case boolean areEqualCaseInsensitive = str1.equalsIgnoreCase(str2); System.out.println("Case-insensitive comparison of \"" + str1 + "\" and \"" + str2 + "\": " + areEqualCaseInsensitive); // Comparing str1 and str3 ignoring case boolean areEqualStr1Str3 = str1.equalsIgnoreCase(str3); System.out.println("Case-insensitive comparison of \"" + str1 + "\" and \"" + str3 + "\": " + areEqualStr1Str3); } }In this code:
- We declare three
Stringvariables:str1,str2, andstr3. str1.equals(str2)performs a case-sensitive comparison. It will returnfalsebecause "Hello" and "hello" are different due to the uppercase 'H'.str1.equalsIgnoreCase(str2)performs a case-insensitive comparison. It will returntruebecause "Hello" and "hello" are the same when case is ignored.- We also compare
str1andstr3ignoring case to show that different words are still considered different.
- We declare three
Save the file (Ctrl+S or Cmd+S).
Open the Terminal at the bottom of the WebIDE. Make sure you are in the
~/projectdirectory.Compile the Java program using the
javaccommand:javac CaseInsensitiveCompare.javaIf there are no errors, you will see no output. A
CaseInsensitiveCompare.classfile will be created in the~/projectdirectory.Run the compiled Java program using the
javacommand:java CaseInsensitiveCompareYou should see the following output:
Case-sensitive comparison of "Hello" and "hello": false Case-insensitive comparison of "Hello" and "hello": true Case-insensitive comparison of "Hello" and "World": falseThis output confirms that
equals()is case-sensitive, whileequalsIgnoreCase()ignores case.
Using equalsIgnoreCase() is a convenient way to compare strings without worrying about whether letters are uppercase or lowercase.
Use Character.isLowerCase() in Loop
In the previous step, we learned how to compare entire strings while ignoring case. Sometimes, you might need to examine individual characters within a string and check if they are lowercase or uppercase. Java provides helpful methods for this in the Character class.
In this step, we will use the Character.isLowerCase() method within a loop to count the number of lowercase letters in a string.
Open the WebIDE if it's not already open.
In the File Explorer on the left, make sure you are in the
~/projectdirectory.Create a new file named
CountLowercase.javain the~/projectdirectory.Open the
CountLowercase.javafile in the editor.Copy and paste the following code into the editor:
public class CountLowercase { public static void main(String[] args) { String text = "Hello World 123!"; int lowercaseCount = 0; // Loop through each character in the string for (int i = 0; i < text.length(); i++) { char character = text.charAt(i); // Get the character at the current index // Check if the character is a lowercase letter if (Character.isLowerCase(character)) { lowercaseCount++; // Increment the counter if it's lowercase } } System.out.println("The string is: \"" + text + "\""); System.out.println("Number of lowercase letters: " + lowercaseCount); } }Let's break down this code:
- We have a
Stringvariabletextcontaining a mix of uppercase letters, lowercase letters, numbers, and symbols. - We initialize an integer variable
lowercaseCountto 0. This variable will store the count of lowercase letters. - We use a
forloop to iterate through each character of the string. The loop runs from index 0 up to (but not including) the length of the string. text.charAt(i)gets the character at the current indexi.Character.isLowerCase(character)is a method that returnstrueif the givencharacteris a lowercase letter, andfalseotherwise.- If
Character.isLowerCase()returnstrue, we incrementlowercaseCount. - Finally, we print the original string and the total count of lowercase letters.
- We have a
Save the file (Ctrl+S or Cmd+S).
Open the Terminal at the bottom of the WebIDE. Make sure you are in the
~/projectdirectory.Compile the Java program:
javac CountLowercase.javaIf the compilation is successful, a
CountLowercase.classfile will be created.Run the compiled Java program:
java CountLowercaseYou should see the following output:
The string is: "Hello World 123!" Number of lowercase letters: 8The output shows that there are 8 lowercase letters in the string "Hello World 123!" ('e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd').
Using Character.isLowerCase() within a loop allows you to perform character-by-character analysis of a string, which is useful for various text processing tasks.
Ignore Non-Letter Characters
In the previous step, we counted lowercase letters. However, strings can contain various characters like numbers, spaces, and symbols. When comparing strings or analyzing their content, you often want to ignore these non-letter characters.
In this step, we will modify our program to process only letter characters and ignore everything else. We will use the Character.isLetter() method for this.
Open the WebIDE if it's not already open.
Open the
CountLowercase.javafile that you created in the previous step.Modify the code to include a check for whether a character is a letter before checking if it's lowercase. Replace the existing code with the following:
public class CountLowercase { public static void main(String[] args) { String text = "Hello World 123!"; int lowercaseCount = 0; StringBuilder lettersOnly = new StringBuilder(); // To store only letter characters // Loop through each character in the string for (int i = 0; i < text.length(); i++) { char character = text.charAt(i); // Get the character at the current index // Check if the character is a letter if (Character.isLetter(character)) { lettersOnly.append(character); // Add the letter to our new string // Check if the letter is lowercase if (Character.isLowerCase(character)) { lowercaseCount++; // Increment the counter if it's lowercase } } } System.out.println("The original string is: \"" + text + "\""); System.out.println("Letters only string: \"" + lettersOnly.toString() + "\""); System.out.println("Number of lowercase letters (ignoring non-letters): " + lowercaseCount); } }Here's what we added and changed:
- We introduced a
StringBuildercalledlettersOnly.StringBuilderis used for efficiently building strings, especially when you are adding characters in a loop. - Inside the loop, we added an
if (Character.isLetter(character))condition. This checks if the currentcharacteris a letter (either uppercase or lowercase). - If the character is a letter, we append it to the
lettersOnlyStringBuilderusinglettersOnly.append(character). - The
Character.isLowerCase(character)check is now nested inside theCharacter.isLetter()check, ensuring we only count lowercase letters among the characters that are actually letters. - Finally, we print the original string, the string containing only letters, and the count of lowercase letters among those letters.
- We introduced a
Save the file (Ctrl+S or Cmd+S).
Open the Terminal at the bottom of the WebIDE. Make sure you are in the
~/projectdirectory.Compile the modified Java program:
javac CountLowercase.javaEnsure there are no compilation errors.
Run the compiled Java program:
java CountLowercaseYou should see the following output:
The original string is: "Hello World 123!" Letters only string: "HelloWorld" Number of lowercase letters (ignoring non-letters): 8Notice that the "Letters only string" now contains only "HelloWorld", and the lowercase count is still 8, as we are only counting lowercase letters among the actual letters.
Using Character.isLetter() is very useful when you need to filter out non-alphabetic characters from a string before processing it further.
Summary
In this lab, we learned how to check if a string is in lowercase in Java. We explored two primary methods. First, we compared strings using equalsIgnoreCase() to perform case-insensitive comparisons, demonstrating how to check if two strings are the same regardless of letter case.
Second, although the full details were not provided, the steps indicate we would learn to iterate through a string using a loop and utilize Character.isLowerCase() to check if individual characters are lowercase, while also considering how to ignore non-letter characters during this process.



