当前位置: 代码迷 >> J2ME >> 小弟求教java初级有关问题
  详细解决方案

小弟求教java初级有关问题

热度:538   发布时间:2013-02-25 21:33:49.0
小弟求教java初级问题!
运用java程序将“asdJKL234+-*"变成"ASDjkl234***"谢谢 


------解决方案--------------------------------------------------------
Java code
public class Test {        // 0-9的ASCII码:48-57    // a-z的ASCII码:97-122    // A-Z的ASCII码:65-90     public static void main(String[] args) {        String str = "asdJKL234+-*";        char[] ch  = str.toCharArray();        for (int i = 0; i < ch.length; i++) {            if(ch[i]>=65 && ch[i]<=90){                ch[i]=(char)(ch[i]+32);            }else if(ch[i]>=97 && ch[i]<=122){                ch[i]=(char)(ch[i]-32);            }else if(ch[i]<48 || ch[i]>57){                ch[i] = '*';            }        }        String newStr = new String(ch);        System.out.println(newStr);    }}
------解决方案--------------------------------------------------------
for example
Java code
String s = "asdJKL234+-*";StringBuilder buf = new StringBuilder();for (char c : s.toCharArray()) {    if (c>='a' && c<='z') {        buf.append((char)(c+('A'-'a')));    } else if (c>='A' && c<='Z') {        buf.append((char)(c+('a'-'A')));    } else if (c>='0' && c<='9'){        buf.append(c);    } else {        buf.append("*");    }}String result = buf.toString();System.out.println(result);
------解决方案--------------------------------------------------------
String s = "asdJKL234+-*";
StringBuilder buf = new StringBuilder();
for (char c : s.toCharArray()) {
if (c>='a' && c<='z') {
buf.append((char)(c+('A'-'a')));
} else if (c>='A' && c<='Z') {
buf.append((char)(c+('a'-'A')));
} else if (c>='0' && c<='9'){
buf.append(c);
} else {
buf.append("*");
}
}
String result = buf.toString();
System.out.println(result);

------解决方案--------------------------------------------------------
Java code
    public String format(String str ){        StringBuffer sb = new StringBuffer();        char c[] = str.toCharArray();        //得到字符串的字节数组        for(char temp : c){            String s = new Character(temp).toString();    //取出一个字符转换成String类型            if(s.matches("\\p{Lower}")){    //如果该字符是小写的就转换成大写的                s = s.toUpperCase();            }else if(s.matches("\\p{Upper}")){     //如果该字符是是大写的,就转换成小写的                s = s.toLowerCase();            }else if(s.matches("\\d")){                //不用做什么            }else{        //将非数字和字母的字符变成*                s = "*";            }            sb.append(s);        }            return sb.toString();    }
  相关解决方案