toLowerCase()方法將String轉換為小寫。如果字符串中沒有應該被轉換的字符,則將原字符串返回,否則返回一個新的字符串。
語法:str.toLowerCase()
toUpperCase()方法將Srtring轉換為大寫。如果字符串中沒有應該轉換的字符,則將原字符串返回,否則返回一個新的字符串。
語法:str.toUpperCase()
說明:使用toLowerCase()方法和toUpperCase()方法進行大小寫轉換時,數字或非字符不受影響。
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); } }