- 转小写:
ch | 0b00100000
即ch | 0x20
即ch | 32
效果:A-Z
-> a-z
,小写字母、数字、大部分标点符号不变。_
变成了奇怪的字符。
测试:
public static void main(String[] args) {
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "1234567890"
+ "[]\;',./"
+ "!@#$%^&*()_+{}|:"<>?";
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = (char) (chars[i] | 0b00100000);
System.out.print(c);
}
}
- 转大写:
ch & 0b11011111
即ch & 0xDF
即ch & 223
效果:a-z
-> A-Z
,小写字母、小部分标点符号不变。数字和大部分标点符号变成了奇怪的字符。
测试:
public static void main(String[] args) {
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "1234567890"
+ "[]\;',./"
+ "!@#$%^&*()_+{}|:"<>?";
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = (char) (chars[i] & 0b11011111);
System.out.print(c);
}
}
- 大小写翻转:
ch ^ 32
效果:a-z
-> A-Z
,A-Z
-> a-z
。数字和大部分标点符号变成了奇怪的字符。
测试:
public static void main(String[] args) {
String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "1234567890"
+ "[]\;',./"
+ "!@#$%^&*()_+{}|:"<>?";
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = (char) (chars[i] ^ 32);
System.out.print(c);
}
}