Java String toLowerCase() Method

Java String toLowerCase() Method

What is string in java?

A String is a sequence of characters. Strings are immutable, which means that once a string is created, it cannot be modified.There are many methods available in the String class that you can use to manipulate strings. For example, you can use the length() method to get the length of a string, the charAt() method to get the character at a specific index in the string, and the substring() method to get a sub-string of the original string.

  • Strings are used frequently in Java programs to represent text, such as user input, program output, and data stored in text files.

To know more about String Class in java read the complete article.

String toLowerCase() Method:

The toLowerCase() method is a method of the String class in the Java programming language that returns a new string with all the characters in the original string converted to lowercase, based on the rules of the default locale.

  • The toLowerCase() method does not modify the original string. Instead, it creates a new string that contains the lowercase version of the characters in the original string.

The toLowerCase() method is useful for converting strings to a consistent case, especially when comparing strings or storing them in a data structure such as a database. It can also be used to make strings easier to read or more visually appealing.

Syntax:

public String toLowerCase()

Throws:

The toCharArray() method does not throw any exceptions.

Let’s look at a string-related Java program where the Java String toLowerCase() Method is used to perform an operation on the given string.

Example 1 : Java String toLowerCase () Method

Run
public class Main {
  public static void main(String[] args) {
    String txt = "PREPINSTA or PREPINSTA PRIME";
    System.out.println(txt.toLowerCase());
  }
}

Output

prepinsta or prepinsta prime

Example 2 : Java string tolowerCase Method

Run
public class Main {
  public static void main(String[] args) {
    String str = "HELLO WORLD";
    String lowerCaseStr = str.toLowerCase();

    // Count the number of vowels in the lowercase string
    int vowelCount = 0;
    for (int i = 0; i < lowerCaseStr.length(); i++) {
      char c = lowerCaseStr.charAt(i);
      if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
        vowelCount++;
      }
    }

    System.out.println("Number of vowels: " + vowelCount); // prints "Number of vowels: 3"
  }
}

Output

Number of vowels: 3

Example 3: Java String toLowerCase Method

Run
public class Main {
  public static void main(String[] args) {
    String str1 = "HELLO WORLD";
    String str2 = "hello world";

    // Convert both strings to lowercase and compare them
    if (str1.toLowerCase().equals(str2.toLowerCase())) {
      System.out.println("The strings are equal (ignoring case)");
    } else {
      System.out.println("The strings are not equal (ignoring case)");
    }
  }
}

Output

The strings are equal (ignoring case)

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription