简体   繁体   English

仅在Java字符串中删除括号

[英]remove parenthesis alone in the java string

In my java string eg:hello(world) is there. 在我的java字符串中,例如:hello(world)在那里。 I want to remove the paranthesis alone the result should be helloworld . 我只想删除括号,结果应该是helloworld

I tryied the regexpression like this 我试着这样的正则表达式

String strFunction = Hello(world);

strFunction.replaceAll("\\)\\(\\s+", "");

But it is notworking,please help it out 但这不起作用,请帮忙

Strings are immutable, so you need to re-assign the result back to the original string. 字符串是不可变的,因此您需要将结果重新分配回原始字符串。 Also, there is some issue with your regex. 另外,您的正则表达式也存在一些问题。 Currently it will try to replace those brackets if they occur back to back. 当前,如果它们背靠背出现,它将尝试替换这些支架。

You need to use a character class: 您需要使用一个字符类:

strFunction = strFunction.replaceAll("[)(]", "");
strFunction = strFunction.replaceAll("[(\\s)]", "");
  • Assign the result back to your string reference. 将结果分配回您的字符串引用。
  • Use a character class [] . 使用字符类[] (Note: you don't need to escape () in there.) (注意:您无需在其中转义() 。)

它消除所有括号,无论它们是否平衡。

String replaced = "(4+5)+6".replaceAll("[()]", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM