我想从我的 java 字符串中删除 [、]、( 和 )。
我尝试过 .replaceAll("(", "");
和 .replaceAll("[", "");
但它不起作用。在我的程序中str 从文件中读取。
String str = "I [need] this ]message () without (this[])";
最佳答案
您可能有解决方案,但 replaceAll
方法不会修改字符串。将结果保存在变量中:
String str = "I [need] this ]message () without (this[])".replaceAll("[()\\[\\]]", "");
参数“[()\\[\\]]”
是一个正则表达式。外部[]
表示:内部符号之一。它们是 (
和 )
。此外,您希望匹配 [
和 ]
。但是您必须在它们之前放置 \\
因为它们具有特殊含义(“其中之一”)。
关于java - 如何从java字符串中删除[]和(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35530674/