How to use the toLowerCase() method of the String class to convert a string to lowercase
When processing strings, sometimes we need to convert the string to lowercase. The toLowerCase() method in the String class can help us achieve this function. This article will introduce how to use this method to convert strings to lowercase.
First of all, we need to understand the basic usage of toLowerCase() method. The function of this method is to convert the alphabetic characters in the String object to lowercase and return a new String object. The original String object will not change.
The following is a basic example code for converting a string to lowercase using the toLowerCase() method:
public class Main { public static void main(String[] args) { String str = "Hello World"; String strLower = str.toLowerCase(); System.out.println("原始字符串: " + str); System.out.println("小写字符串: " + strLower); } }
The output result is:
原始字符串: Hello World 小写字符串: hello world
By calling the toLowerCase() method, we can See that the original string "Hello World" is converted into the lowercase form "hello world".
In addition to the basic usage, the toLowerCase() method can also be used with other string manipulation methods to process strings more flexibly. Next, we'll cover a few common scenarios.
When comparing strings, we usually convert the strings into uniform upper and lower case for accurate comparison. Use the toLowerCase() method to convert strings to lowercase and then compare them.
public class Main { public static void main(String[] args) { String str1 = "hello"; String str2 = "HELLO"; if (str1.toLowerCase().equals(str2.toLowerCase())) { System.out.println("两个字符串相等"); } else { System.out.println("两个字符串不相等"); } } }
The output result is:
两个字符串相等
In this example, we first convert str1 and str2 to lowercase respectively, and then use the equals() method for comparison. Since their lowercase forms are the same, the output is "Both strings are equal".
Sometimes we need to count the number of occurrences of specific letters in a string. Use the toLowerCase() method to convert a string to lowercase, thus ignoring differences in the case of letters.
public class Main { public static void main(String[] args) { String str = "How are you?"; char target = 'o'; int count = 0; for (char c : str.toLowerCase().toCharArray()) { if (c == target) { count++; } } System.out.println("字母'o'出现的次数为:" + count); } }
The output result is:
字母'o'出现的次数为:2
In this example, we first convert the string to lowercase, then traverse the character array and count the number of occurrences of the letter 'o'.
Through the above example, we learned how to use the toLowerCase() method of the String class to convert a string to lowercase. Whether it is string comparison or letter statistics, this method will help us handle string operations more flexibly. Hope this article is helpful to everyone!
The above is the detailed content of How to convert a string to lowercase using toLowerCase() method of String class. For more information, please follow other related articles on the PHP Chinese website!