步步人生
2013-12-19 08:34
采纳率: 100%
浏览 7.3k
已采纳

java如何将字符串的所有括号中指定字符替换掉?(正则表达式或者其它方法)

要处理字符串

Medical Care ( Costs Inverse ## agonism ( Costs Inverse ## agonism ) ) Costs ( Inverse ## agonism ) at beta_1 ##adrenergic receptors
替换操作:将字符串中 所有括号 内的 ##替换成 @
处理结果:
Medical Care ( Costs Inverse @ agonism ( Costs Inverse @ agonism ) ) Costs ( Inverse @ agonism ) at beta_1 ## adrenergic receptors
各路高手帮帮忙 正则表达式或者其它方法都行的

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • a_jian 2013-12-19 09:58
    已采纳
    public static void main(String[] args) {
        String s = "Medical Care ( Costs Inverse ## agonism ( Costs Inverse ## agonism ) ) Costs ( Inverse ## agonism ) at beta_1 ##adrenergic receptors";
        String returnValue = "";
        String [] str =   s.split("\\)");
    
        for (int i = 0; i < str.length; i++) {
            if(" ".equals(str[i])){
                str[i] = ")";
            }
    
            if(str[i].contains("(")){
                str[i] = str[i].replaceAll("##", "@@")+")";
            }
        }
    
        s = ArrayUtils.toString(str).replace(",", " ");
        returnValue = s.substring(1, s.length()-1);
       System.out.println(returnValue);
    }
    
    打赏 评论

相关推荐 更多相似问题