繁体   English   中英

仅在Java字符串中删除括号

[英]remove parenthesis alone in the java string

在我的java字符串中,例如:hello(world)在那里。 我只想删除括号,结果应该是helloworld

我试着这样的正则表达式

String strFunction = Hello(world);

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

但这不起作用,请帮忙

字符串是不可变的,因此您需要将结果重新分配回原始字符串。 另外,您的正则表达式也存在一些问题。 当前,如果它们背靠背出现,它将尝试替换这些支架。

您需要使用一个字符类:

strFunction = strFunction.replaceAll("[)(]", "");
strFunction = strFunction.replaceAll("[(\\s)]", "");
  • 将结果分配回您的字符串引用。
  • 使用字符类[] (注意:您无需在其中转义() 。)

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

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

暂无
暂无

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

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