给定一个字符串,比如String s = new String("ajld243DKJkjd85UYOjYhggI");
如何将字符串s中的字母小写转换成大写,大写转换成小写?
请给出详细代码作者: toShareBeauty 时间: 2013-7-28 13:06 本帖最后由 toShareBeauty 于 2013-7-28 13:12 编辑
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = new String("ajld243DKJkjd85UYOjYhggI");
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if ('a' <= c && c <= 'z') {
c = Character.toUpperCase(c);
}else if ('A' <= c && c <= 'Z') {
c = Character.toLowerCase(c);
}
buffer.append(c);
}
System.out.println(buffer.toString());
}
复制代码
如果你是 入学考试,最好自己做哈,这么简单的如果不会,估计后面跟不上哦。作者: jialihong 时间: 2013-7-28 14:06
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = new String("adjieEDD2kllk");
StringBuffer buffer = new StringBuffer();
for(int i=0;i<s.length();i++){
char c = s.charAt(i);
if(c>=65&&c<=90)
c = Character.toLowerCase(c);
else if(c>=97&&c<=122){
c = Character.toUpperCase(c);
}
buffer.append(c);
}
System.out.print(buffer.toString());
} 作者: の放下执著 时间: 2013-7-28 19:33
public class transDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = new String("ajld243DKJkjd85UYOjYhggI");
s = trans(s);
System.out.println(s);
}
public static String trans(String str)//把功能封装成一个函数
{
char[] arr = str.toCharArray();//把字符串转成字符数组,容易逐个操作
StringBuilder sb = new StringBuilder();//定义一个容器,用于接收字符