当前位置:编程学习 > JAVA >>

关于java中string的处理

有一字符串是这样的:String str1="abc中国123@";我想将这样的字符串转换成:String str2="abc123"也就是说,转换后的字符串只要“字母”和“数字”,求一方法. --------------------编程问答-------------------- String str = "abc中国123@";
str = str.replaceAll("[^\\w]", ""); --------------------编程问答-------------------- String str = "abc中国123@";

StringBuffer buffer = new StringBuffer();

for(int i = 0; i < str.length(); i++) {
String temp = str.charAt(i) + "";
if(temp.matches("[\\w]") || temp.matches("[\\d]")) {
buffer.append(temp);
}
}

System.out.println(buffer.toString()); --------------------编程问答-------------------- 运用正则表达式了 --------------------编程问答--------------------
引用 1 楼 uyerp 的回复:
String str = "abc中国123@";
str = str.replaceAll("[^\\w]", "");

+1 --------------------编程问答--------------------
引用 1 楼 uyerp 的回复:
String str = "abc中国123@";
str = str.replaceAll("[^\\w]", "");


+1 --------------------编程问答--------------------
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,