lowercase
Java String to lowercase conversion can be done using toLowerCase()
method.
可以使用toLowerCase()
方法将Java String转换为小写字母。
Java String to LowerCase (Java String to LowerCase)
- Java String
toLowerCase()
method has two variants –toLowerCase()
andtoLowerCase(Locale locale)
. Java字符串toLowerCase()
方法有两个变体–toLowerCase()
和toLowerCase(Locale locale)
。 - Conversion of the characters to lower case is done by using the rules of default locale. Calling
toLowerCase()
function is same as callingtoLowerCase(Locale.getDefault())
. 字符转换为小写是通过使用默认语言环境的规则完成的。 调用toLowerCase()
函数与调用toLowerCase(Locale.getDefault())
。 - Java String to lower case method is locale sensitive, so use it carefully with strings that are intended to be used locale independently. For example programming language identifiers, HTML tags, protocol keys etc. Otherwise you might get unwanted results. Java字符串到小写字母的方法对语言环境敏感,因此请谨慎地将其与打算独立用于语言环境的字符串一起使用。 例如编程语言标识符,HTML标记,协议密钥等。否则,您可能会得到不想要的结果。
- To get correct lower case results for locale insensitive strings, use
toLowerCase(Locale.ROOT)
method. 若要获取不区分区域设置的字符串的正确小写结果,请使用toLowerCase(Locale.ROOT)
方法。 - String
toLowerCase()
returns a new string, so you will have to assign that to another string. The original string remains unchanged because Strings are immutable. 字符串toLowerCase()
返回一个新字符串,因此您必须将其分配给另一个字符串。 原始字符串保持不变,因为字符串是不可变的 。 - If locale is passed as
null
totoLowerCase(Locale locale)
method, then it will throw NullPointerException. 如果将locale作为null
传递到toLowerCase(Locale locale)
方法,则它将抛出NullPointerException 。
Java String to LowerCase示例 (Java String to LowerCase Example)
Let’s see a simple example to convert a string to lower case and print it.
让我们看一个简单的示例,将字符串转换为小写并打印。
String str = "Hello World!";
System.out.println(str.toLowerCase()); //prints "hello world!"
We can also use Scanner class to get user input and then convert it to lower case and print it.
我们还可以使用Scanner类获取用户输入,然后将其转换为小写并打印。
Here is a complete example program to convert java string to lowercase and print it.
这是将Java字符串转换为小写并打印的完整示例程序。
package com.journaldev.string;
import java.util.Scanner;
public class JavaStringToLowerCase {
public static void main(String[] args) {
String str = "JournalDev";
String strLowerCase = str.toLowerCase();
System.out.println("Java String to Lower Case Example Output: " + strLowerCase);
readUserInputAndPrintInLowerCase();
}
private static void readUserInputAndPrintInLowerCase() {
Scanner sc = new Scanner(System.in);
System.out.println("Please provide input String and press Enter:");
String str = sc.nextLine();
System.out.println("Input String in Lower Case = " + str.toLowerCase());
sc.close();
}
}
Below image shows the output from a sample execution of above program.
下图显示了上述程序的示例执行的输出。
That’s all for java string to lowercase conversion example.
这就是java字符串到小写转换示例的全部内容。
Reference: API Doc
参考: API文档
翻译自: https://www.journaldev.com/18292/java-string-to-lowercase
lowercase