Java String toLowerCase() 与示例
Java String toLowerCase()方法用于字符串,我们希望将字符串中的所有字母转换为小写。
toLowerCase()方法有两种类型,如下所示:
- toLowerCase()
- toLowerCase(Locale loc):使用给定 Locale 的规则将所有字符转换为小写。
Note: Internally toLowerCase() works in similar way as toLowerCase(Locale.getDefault()) method.
句法:
public String toLowerCase()
public String toLowerCase(Locale loc)
该方法的封装视图如下:
--> java.util Package
--> String Class
--> toLowerCase() Method
参数:要应用的区域设置值
返回类型:以小写字母返回字符串
示例 1:
java
// Java Program to Demonstrate Working of
// toLowerCase() Method of String Class
// Importing required classes
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input string
String s = "Geeks for Geeks";
// Converting string s to lowercase letter
String s2 = s.toLowerCase();
// Printing the lowercase string corresponding
// to input string
System.out.println(s2);
}
}
java
// Java program to demonstrate Working of toLowerCase()
// Method present inside Locale class
// Importing required classes
import java.util.Locale;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input string
String s = "I Know YOI Bui You Don't Know ME";
// Locales with the language "tr" for TURKISH
//"en" for ENGLISH is created
Locale TURKISH = Locale.forLanguageTag("tr");
Locale ENGLISH = Locale.forLanguageTag("en");
// Converting string s to lowercase letter
// using TURKISH and ENGLISH language
String gfg2 = s.toLowerCase(TURKISH);
String gfg3 = s.toLowerCase(ENGLISH);
// Printing above string
System.out.println(gfg2);
System.out.println(gfg3);
}
}
输出
geeks for geeks
示例 2:
Java
// Java program to demonstrate Working of toLowerCase()
// Method present inside Locale class
// Importing required classes
import java.util.Locale;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input string
String s = "I Know YOI Bui You Don't Know ME";
// Locales with the language "tr" for TURKISH
//"en" for ENGLISH is created
Locale TURKISH = Locale.forLanguageTag("tr");
Locale ENGLISH = Locale.forLanguageTag("en");
// Converting string s to lowercase letter
// using TURKISH and ENGLISH language
String gfg2 = s.toLowerCase(TURKISH);
String gfg3 = s.toLowerCase(ENGLISH);
// Printing above string
System.out.println(gfg2);
System.out.println(gfg3);
}
}
输出
? know yo? bui you don't know me
i know yoi bui you don't know me