如何删除字符串中的括号字符(java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25852961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:18:21  来源:igfitidea点击:

how to remove brackets character in string (java)

javaregexstringbrackets

提问by Muhammad Haryadi Futra

I want to remove all type of brackets character (example: [],(),{}) in string by using java.

我想使用 java 删除字符串中所有类型的括号字符(例如:[],(),{})。

I tried using this code:

我尝试使用此代码:

String test = "watching tv (at home)"; 
test = test.replaceAll("(","");
test = test.replaceAll(")","");

But it's not working, help me please.

但它不起作用,请帮助我。

采纳答案by StackFlowed

To remove all punctuation marks that include all brackets, braces and sq. brackets ... as per the question is:

要删除包括所有括号、大括号和方括号的所有标点符号......根据问题是:

String test = "watching tv (at home)"; 
test = test.replaceAll("\p{P}","");

回答by AbstractChaos

You need to escape the bracket as it will be treated as part of a regex

您需要转义括号,因为它将被视为正则表达式的一部分

String test = "watching tv (at home)"; 
test = test.replaceAll("\(","");
test = test.replaceAll("\)","");

Also to remove all brackets try

还要删除所有括号尝试

String test = "watching tv (at home)"; 
test = test.replaceAll("[\(\)\[\]\{\}]","");

回答by Reimeus

The first argument of replaceAlltakes a regular expression.

的第一个参数replaceAll采用正则表达式。

All the brackets have meaning in regex: Brackets are used in regex to reference capturing groups, square brackets are used for character class& braces are used for matched character occurrence. Therefore they all need to be escaped...However here the characters can simply be enclosed in a character classwith just escaping required for square brackets

所有括号在正则表达式中都有意义:括号在正则表达式中用于引用捕获组,方括号用于字符类,大括号用于匹配的字符出现。因此,它们都需要转义……然而,这里的字符可以简单地包含在一个字符类中,只需对方括号进行转义即可

test = test.replaceAll("[\[\](){}]","");

回答by Javi Fernández

You can use String.replaceinstead of String.replaceAllfor better performance, as it searches for the exact sequence and does not need regular expressions.

您可以使用String.replace而不是String.replaceAll为了更好的性能,因为它搜索确切的序列并且不需要正则表达式。

String test = "watching tv (at home)"; 
test = test.replace("(", " ");
test = test.replace(")", " ");
test = test.replace("[", " ");
test = test.replace("]", " ");
test = test.replace("{", " ");
test = test.replace("}", " ");

If you are working with texts I recommend you to replace the brackets with an empty space to avoid words joining together: watching tv(at home) -> watching tvat home

如果您正在处理文本,我建议您将括号替换为空格以避免单词连接在一起: watching tv(at home) -> watching tvat home

回答by hwnd

The first argument passed to the replaceAll()method should be a regular expression. If you want to match those literal bracket characters you need to escape \\(, \\)them.

传递给该replaceAll()方法的第一个参数应该是一个正则表达式。如果你想匹配那些需要转义的文字括号字符\\(\\)它们。

You could use the following to remove bracket characters. Unicode property \p{Ps}will match any kind of opening bracket and Unicode property \p{Pe}matches any kind of closing bracket.

您可以使用以下内容删除括号字符。Unicode 属性\p{Ps}将匹配任何类型的左括号,而Unicode 属性将\p{Pe}匹配任何类型的右括号。

String test = "watching tv (at home) or [at school] or {at work}()[]{}";
test = test.replaceAll("[\p{Ps}\p{Pe}]", "");
System.out.println(test); //=> "watching tv at home or at school or at work"

回答by Santhosh Hirekerur

subject =StringUtils.substringBetween(subject, "[", "]")

主题 =StringUtils.substringBetween(主题,“[”,“]”)