字符

小写转大写

1
2
char c1 = 'a';
c1=Character.toUpperCase(c1);

大写转小写

1
2
char c2 = 'B'; 
c2 = Character.toLowerCase(c2);

字符串

全部转换成小写

1
2
String str = "aBcD";
str=str.toLowerCase();

全部转换成大写

1
2
String str = "aBcD";
str=str.toUpperCase();

Java代码

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);
}

运行结果

img