ps:java大小写之间转换练习
1.使用封装类型
Character.toUpperCase(temp)
的大小写转换函数
public String convertString(String str) { StringBuffer sBuffer = new StringBuffer(); for (int i = 0; i < str.length(); i++) { char temp = str.charAt(i); if (temp >= 'a' && temp <= 'z') { sBuffer.append(Character.toUpperCase(temp)); } else { sBuffer.append(Character.toLowerCase(temp)); } } return sBuffer.toString(); }
2.使用ASCII表。小写比大写的值大32.
public String convertByAscii(String str) { char[] chs = str.toCharArray(); for (int i = 0; i < chs.length; i++) { char temp = chs[i]; if (temp <= 'a' && temp >= 'z') {//小写转大写 chs[i] = (char) (temp - 32);//需要强制从char转为int } else if (temp <= 'A' && temp >= 'Z') { chs[i] = (char) (temp + 32); } } return String.valueOf(chs); }
https://blog.csdn.net/jiejinquanil/article/details/51166883 查看ascii表