Java 字符串去除括号内字符 - 代码示例
可以使用正则表达式来实现字符串去掉括号内的字符,具体代码如下:
public static String removeParentheses(String str) {
return str.replaceAll('\\(.*?\\)', '');
}
其中,'\('和'\)'表示左右括号,'.*?'表示匹配任意字符,'?'表示非贪婪匹配,即最小匹配。
测试代码如下:
public static void main(String[] args) {
String str = 'hello(world), this is (a) test.';
System.out.println(removeParentheses(str)); // 输出:hello, this is test.
}
原文地址: https://www.cveoy.top/t/topic/oLeL 著作权归作者所有。请勿转载和采集!