Home> Java> javaTutorial> body text

Convert a string to lowercase using java's String.toLowerCase() function

WBOY
Release: 2023-07-24 23:52:59
Original
1239 people have browsed it

String.toLowerCase() function is a very useful and common string processing function in Java, which can convert a string to lowercase form. In this article, we will introduce how to use this function and give some related code examples.

First, let us take a look at the basic syntax of the String.toLowerCase() function. It doesn't have any parameters, just call it. The following is a sample code:

String str = "Hello World"; String lowercaseStr = str.toLowerCase(); System.out.println(lowercaseStr);
Copy after login

The output result will be:

hello world

It can be seen that the function converts all uppercase letters in the original string into lowercase letters, and returns a new string.

In practical applications, we often need to convert the input string into lowercase for processing. For example, when users enter their username or password, we usually convert them into lowercase to avoid verification problems caused by uppercase and lowercase. The following is a code example for user login:

import java.util.Scanner; public class LoginSystem { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入用户名:"); String username = scanner.nextLine(); System.out.print("请输入密码:"); String password = scanner.nextLine(); // 将用户名和密码转换为小写形式 String lowercaseUsername = username.toLowerCase(); String lowercasePassword = password.toLowerCase(); // 进行验证 if (lowercaseUsername.equals("admin") && lowercasePassword.equals("password")) { System.out.println("登录成功!"); } else { System.out.println("用户名或密码错误!"); } } }
Copy after login

In the above code, we first use the Scanner class to obtain the user name and password entered by the user. Then, use the String.toLowerCase() function to convert them to lowercase. Finally, we verify the username and password. In this way, regardless of whether the user enters uppercase or lowercase letters, the correct result will be obtained.

In addition to user login to the system, the String.toLowerCase() function can also be used to compare whether strings are equal. The following is a code example:

String str1 = "HELLO"; String str2 = "hello"; if (str1.toLowerCase().equals(str2.toLowerCase())) { System.out.println("两个字符串相等!"); } else { System.out.println("两个字符串不相等!"); }
Copy after login

The output result is:

The two strings are equal!

By calling the String.toLowerCase() function, we can avoid sensitivity to string case and provide more flexible and accurate string comparison.

In programming, string processing is one of the common tasks. The String.toLowerCase() function provides us with a simple and convenient way to convert strings to lowercase, making string processing more flexible and easier to operate. Whether it is user-entered strings, string comparisons, or other application scenarios, the String.toLowerCase() function can come in handy. I hope this article can help you better understand and use this important string processing function.

The above is the detailed content of Convert a string to lowercase using java's String.toLowerCase() function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!