//把一个字符串中的大写转为小写,小写转换为大写:思路1
public static String exChange(String str){
StringBuffer sb = new StringBuffer();
if(str!=null){
for(int i=0;ichar c = str.charAt(i);
if(Character.isUpperCase(c)){
sb.append(Character.toLowerCase(c));
}else if(Character.isLowerCase(c)){
sb.append(Character.toUpperCase(c));
}
}
}
return sb.toString();
}
//把一个字符串中的大写转为小写,小写转换为大写:思路2
public static String exChange2(String str){
for(int i=0;i//如果是小写
if(str.substring(i, i+1).equals(str.substring(i, i+1).toLowerCase())){
str.substring(i, i+1).toUpperCase();
}else{
str.substring(i, i+1).toLowerCase();
}
}
return str;
}
String str1 = "a";
String str2 = "B";
str1 = str1.toUpperCase();
str2 = str2.toLowerCase();
System.out.println("The Upper Case of a is "+str1);
System.out.println("The Lower Case of B is "+str2);
输出结果就是:
The Upper Case of a is A
The Lower Case of B is b