我有一个字符串:
(aeiou 123) word one
如何从括号中删除所有内容,以便只有"单词#34;仍然?
答案 0 :(得分:1)
您可以使用正则表达式,例如:
String str = "(aeiou 123) word one";
str = str.replaceAll("\\([^\\)]*\\)", "").trim();
答案 1 :(得分:0)
public class ParanthesisRemoval {
public static void main(String args[])
{
String test="ab(xy)cd(zw)ef";
boolean modified = true;
while(modified)
{
modified = false;
int indexOpenParanthesis = test.indexOf("(");
int indexClosedParanthesis = test.indexOf(")");
if(indexOpenParanthesis!=-1 && indexClosedParanthesis!=-1 && indexOpenParanthesis<indexClosedParanthesis)
{
int stringLength = test.length();
test = test.substring(0, indexOpenParanthesis)+test.substring(indexClosedParanthesis+1, stringLength);
modified=true;
}
}
System.out.println(test);
}
}
请注意,这不适用于嵌套的parantesis - (()),或者paranthesis未正确配对(()