面试时经常考的------对字符串的操作(三)

/**
 * 假如有字符串“6sabcsssfsfs33” ,用最有快速的方法去掉字符“ab3 “
 * 
 */

Code:
  1. // =======第一种方法======   
  2.     public static void replaceString(String str) {   
  3.         char myChar[] = str.toCharArray();   
  4.         int len = myChar.length;   
  5.         String tempStr = "";   
  6.         for (int i = 0; i < len; i++) {   
  7.             if ('a' == myChar[i] || 'b' == myChar[i] || '3' == myChar[i]) {   
  8.                 myChar[i] = ' ';   
  9.             }   
  10.             String temp = "";   
  11.             temp = myChar[i] + "";   
  12.             tempStr += temp.trim();   
  13.         }   
  14.         System.out.println(tempStr);   
  15.     }   
  16.   
  17.     // =======第二种方法======   
  18.     public static void replaceString2(String str) {   
  19.         char s[] = str.toCharArray();   
  20.         int len = str.length();   
  21.         char string[] = new char[14];   
  22.         StringBuffer sb = new StringBuffer();   
  23.         for (int i = 0; i < len; i++) {   
  24.             if ('a' != s[i] && 'b' != s[i] && '3' != s[i]) {   
  25.                 // string[i] = s[i];   
  26.                 sb.append(s[i]);   
  27.             }   
  28.             // String temp = null;   
  29.             // temp = string[i] + "";   
  30.             // sb.append(temp.trim());   
  31.   
  32.         }   
  33.         System.out.println(sb.toString());   
  34.     }   
  35.   
  36.     // =======第三种方法======   
  37.     public static String replaceString3(String des, String reg) {   
  38.         StringBuilder buf = new StringBuilder(des.length());   
  39.         char[] chars = des.toCharArray();   
  40.         char[] regchars = reg.toCharArray();   
  41.         for (char c : chars) {   
  42.             boolean b = true;   
  43.             for (char regchar : regchars) {   
  44.                 if (c == regchar) {   
  45.                     b = false;   
  46.                     break   
  47.                 }   
  48.             }   
  49.             if (b)   
  50.                 buf.append(c);   
  51.         }   
  52.         return buf.toString();   
  53.     }   
  54.   
  55.     // =======第四种方法======   
  56.     public static void replaceString4(String str) {   
  57.         // 指定为字符串的正则表达式必须首先被编译为此类的实例   
  58.         Pattern pattern = Pattern.compile("[^a|b|3]"); // ^非 ; 边界匹配器"^"行的开头   
  59.         // 然后,可将得到的模式用于创建 Matcher   
  60.         // 对象,依照正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。   
  61.         Matcher matcher = pattern.matcher(str);   
  62.         String temp = "";   
  63.         while (matcher.find()) {   
  64.             temp += matcher.group();// 返回由以前匹配操作所匹配的输入子序列   
  65.         }   
  66.         System.out.println(temp);   
  67.     }   
  68.   
  69.     // =======第五种方法======   
  70.     public static void replaceString5(String str) {   
  71.         StringBuffer sb = new StringBuffer();   
  72.         Scanner scanner = new Scanner(str);   
  73.         // 匹配扫描   
  74.         scanner.findInLine("(//S*)ab(//S*)33(//S*)");   
  75.         // 返回此扫描器所执行的最后扫描操作的匹配结果   
  76.         MatchResult ms = scanner.match();   
  77.         for (int i = 1; i < ms.groupCount(); i++) { // 注意i的初始值   
  78.             sb.append(ms.group(i));   
  79.         }   
  80.         scanner.close();   
  81.         System.out.println(sb);   
  82.     }   
  83.   
  84.     // =======第六种方法======   
  85.     public static String replaceString6(String str, String toRemove) {   
  86.         boolean[] removeMap = new boolean[255];   
  87.         char[] originChars = str.toCharArray();   
  88.         char[] todoChars = toRemove.toCharArray();   
  89.         char[] destChars = null;   
  90.         int strLen = 0;   
  91.   
  92.         // build toRemove chars map   
  93.         for (int i = 0; i < todoChars.length; i++) {   
  94.             removeMap[(todoChars[i] & 0xFF)] = true;   
  95.         }   
  96.   
  97.         for (int i = 0; i < originChars.length; i++) {   
  98.             if (removeMap[(originChars[i] & 0xff)]) {   
  99.                 if (destChars == null) {   
  100.                     destChars = new char[originChars.length - 1];   
  101.                     System.arraycopy(originChars, 0, destChars, 0, i);   
  102.                 }   
  103.   
  104.             } else {   
  105.                 if (destChars != null) {   
  106.                     destChars[strLen] = originChars[i];   
  107.                 }   
  108.                 strLen++;   
  109.             }   
  110.         }   
  111.         return destChars == null ? str : new String(destChars, 0, strLen);   
  112.     }  

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值