Java String toUpperCase() 方法及示例
String类的Java字符串toUpperCase()方法将字符串的所有字符转换为大写字母。 toUpperCase()方法有两种变体。要考虑的关键是 toUpperCase() 方法与 UpperCase(Locale.getDefault()) 方法的工作方式相同,因为使用了内部默认语言环境。
句法:
public String toUpperCase(Locale loc)
public String toUpperCase()
范围:
- 类型 1:要应用的区域设置值,因为它将所有字符转换为
- 类型 2:不适用
返回类型:它以大写字母返回字符串。
Note: Lowercase is done using the rules of the given Locale.
示例 1:
java
// Java Program to Demonstrate Working of toUpperCase()
// method
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input string
String str = "Welcome! to Geeksforgeeks";
// Converting above input string to
// uppercase letters using UpperCase() method
String strup = str.toUpperCase();
// Print the uppercased string
System.out.println(strup);
}
}
java
// Java program to demonstrate Working of toUpperCase()
// method of Locale class
// Importing Locale class from java.util package
import java.util.Locale;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input string
String str = "Geeks for Geeks";
// Locales with the language "tr" for TURKISH
//"en" for ENGLISH is created
Locale TURKISH = Locale.forLanguageTag("tr");
Locale ENGLISH = Locale.forLanguageTag("en");
// Converting string str to uppercase letter
// using TURKISH and ENGLISH language
String strup1 = str.toUpperCase(TURKISH);
String strup2 = str.toUpperCase(ENGLISH);
System.out.println(strup1);
System.out.println(strup2);
}
}
输出
WELCOME! TO GEEKSFORGEEKS
示例 2:
Java
// Java program to demonstrate Working of toUpperCase()
// method of Locale class
// Importing Locale class from java.util package
import java.util.Locale;
// Main class
class GFG {
// Main driver method
public static void main(String args[])
{
// Custom input string
String str = "Geeks for Geeks";
// Locales with the language "tr" for TURKISH
//"en" for ENGLISH is created
Locale TURKISH = Locale.forLanguageTag("tr");
Locale ENGLISH = Locale.forLanguageTag("en");
// Converting string str to uppercase letter
// using TURKISH and ENGLISH language
String strup1 = str.toUpperCase(TURKISH);
String strup2 = str.toUpperCase(ENGLISH);
System.out.println(strup1);
System.out.println(strup2);
}
}
输出
GEEKS FOR GEEKS
GEEKS FOR GEEKS