toLowerCase()方法将String转换为小写。如果字符串中没有应该被转换的字符,则将原字符串返回,否则返回一个新的字符串。
语法:str.toLowerCase()
toUpperCase()方法将Srtring转换为大写。如果字符串中没有应该转换的字符,则将原字符串返回,否则返回一个新的字符串。
语法:str.toUpperCase()
说明:使用toLowerCase()方法和toUpperCase()方法进行大小写转换时,数字或非字符不受影响。
1 2 3 4 5 6 7 8 9 | public class UpAndLower { //创建类 public static void main(String args[]){ //主方法 String str = new String( "abc DEF" ); //创建字符串str String newstr = str.toLowerCase(); //使用toLowerCase()方法实现小写转换 String newstr2 = str.toUpperCase(); //使用toUpperCase()方法实现大写转换 System.out.println(newstr); System.out.println(newstr2); } } |