Java String toLowerCase和toUpperCase方法

来源:这里教程网 时间:2026-02-17 20:06:58 作者:

方法toLowerCase()将String的字符转换为小写字符。它有两个变种:

String toLowerCase(Locale locale):使用指定的Locale定义的规则将字符串转换为小写。

String toLowerCase():相当于toLowerCase(Locale.getDefault())。Locale.getDefault()获取此 Java 虚拟机实例的默认语言环境的当前值。 Java 虚拟机根据主机环境在启动期间设置默认语言环境。如果未明确指定语言环境,则许多语言环境敏感方法使用它。可以使用setDefault()方法更改它。

示例:toLowerCase()方法

import java.util.Locale;public class LowerCaseExample{   public static void main(String args[]){       String str = new String("ABC IS NOT EQUAL TO XYZ");       //Standard method of conversion       System.out.println(str.toLowerCase());       //By specifying Locale       System.out.println(str.toLowerCase(Locale.FRANCE));   }}

输出:

abc is not equal to xyzabc is not equal to xyz

toUpperCase()

与toLowerCase()方法类似,toUpperCase()也有两个变体:
String toUpperCase(Locale locale):它使用指定的Locale定义的规则将字符串转换为大写字符串。
String toUpperCase():与toUpperCase(Locale.getDefault())相当。

示例:toUpperCase()方法

import java.util.Locale;public class UpperCaseExample{   public static void main(String args[]){       String str = new String("this is a test string");       //Standard method of conversion       System.out.println(str.toUpperCase());       //By specifying Locale       System.out.println(str.toUpperCase(Locale.CHINA));   }}

输出:

THIS IS A TEST STRINGTHIS IS A TEST STRING

相关推荐