首页 文章资讯内容详情

Java toUpperCase()方法及其示例

2026-06-04 2 花语

UpperCase()方法将所有字符转换为大写字母。此方法有两个变体。第一个变体使用给定Locale的规则将此String中的所有字符转换为大写。这等效于调用toUpperCase(Locale.getDefault())。

示例

现在让我们看一个例子-

import java.io.*; public class Demo { public static void main(String args[]) { String Str = new String("This is it!"); System.out.print("Return Value :" ); System.out.println(Str.toUpperCase() ); } }

输出结果

Return Value :THIS IS IT!

示例

我们来看另一个实现toUpperCase()方法的示例-

import java.io.*; import java.util.Locale; public class Demo { public static void main(String args[]) { String str = new String("This is it!"); System.out.print("Return Value :" ); System.out.println(str.toUpperCase() ); Locale ENGLISH = Locale.forLanguageTag("en"); String res = str.toUpperCase(ENGLISH); System.out.println(res); } }

输出结果

Return Value :THIS IS IT! THIS IS IT!