1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public static void main(String[] args) { char c1 = 'a'; char c2 = 'B'; System.out.println("字符大小写转换:"); System.out.println("转换前:c1="+c1); c1=Character.toUpperCase(c1); System.out.println("转换后:c1="+c1); System.out.println("转换前:c2="+c2); c2 = Character.toLowerCase(c2); System.out.println("转换后:c2="+c2); System.out.println("字符串全部转换成大写或小写"); String str = "aBcD"; System.out.println("转换前:" + str); System.out.println("转换后:"); System.out.println("全部转换成小写:"+str.toLowerCase()); System.out.println("全部转换成大写:"+str.toUpperCase()); System.out.println("字符串大写变小写,小写变大写:"); StringBuffer stringBuffer = new StringBuffer(str); System.out.println("转换前:"+stringBuffer); char[] arr = new char[stringBuffer.length()]; for (int i = 0; i < arr.length; i++) { arr[i] = stringBuffer.charAt(i); if (65<=arr[i]&&arr[i]<=90) { arr[i] = Character.toLowerCase(arr[i]); } else if (97<=arr[i]&&arr[i]<=122) { arr[i] = Character.toUpperCase(arr[i]); } } stringBuffer.delete(0, stringBuffer.length()); for (int i = 0; i < arr.length; i++) { stringBuffer.append(arr[i]); } System.out.println("转换后:"+stringBuffer); }
|