`

去除字符串的指定字符

阅读更多
假如有字符串“6sabcsssfsfs33” ,用最有快速的方法去掉字符“ab3”,不能用java内置字符串方法(indeOf,substring,replaceAll等)?
class Remove{ 
 public static void main(String args[]) { 
  String str="6sabcsssfsfs33; 
    boolean removeChars[256] = {true};
    removeChars['a'] = false;
    removeChars['b'] = false;
    removeChars['3'] = false;
  StringBuffer sb = new StringBuffer();
    for (char ch: str) {
        if (!removeChars[ch]) sb.append(ch);
    }
    String result = sb.toString();
 } 
} 


或者用正则

String regx="[^a|b|3]";   
String temp="6sabcsssfsfs33";   
Pattern p=Pattern.compile(regx);   
Matcher m=p.matcher(temp);   
if(m.find())   
     System.out.print(m.group());  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics