Java:转换大小写 (toLowerCase()和toUpperCase())

String 类

.toLowerCase() 方法:将字符串中的所有字符全部转换成小写,非字母的字符不受影响。

语法格式如下:

字符串名.toLowerCase() 


.toUpperCase() 方法:将字符串中的所有字符全部转换成大写,非字母的字符不受影响。

语法格式如下:

字符串名.toUpperCase()

例如:

  1. String str="abcDef 哦哦ghijkLMn";
  2. System.out.println(str.toLowerCase()); //输出abcdef 哦哦 ghijklmn
  3. System.out.println(str.toUpperCase()); //输出ABCDEF 哦哦GHIJKLMN

例 1

下面的实例代码演示:如何对字符串应用大写和小写转换。

  1. public static void main(String[] args)
  2. {
  3. String en="The Day You Went Away"; //定义原始字符串
  4. System.out.println("原始字符串:"+en);
  5. System.out.println("使用 toLowerCase() 方法之后为:"+en.toLowerCase());
  6. System.out.println("使用 toUpperCase() 方法之后为:"+en.toUpperCase());
  7.  
  8. en="select id,name,sex,address,birthday from students";
  9. System.out.println("原始字符串:"+en); //定义原始字符串
  10. System.out.println("使用 toLowerCase() 方法之后为:"+en.toLowerCase());
  11. System.out.println("使用 toUpperCase() 方法之后为:"+en.toUpperCase());
  12. }


运行结果如下:

原始字符串:The Day You Went Away

使用 toLowerCase() 方法之后为:the day you went away

使用 toUpperCase() 方法之后为:THE DAY YOU WENT AWAY

猜你喜欢

转载自blog.csdn.net/weixin_44015669/article/details/90112461