2015-11-26 181 views
-1

我有这样的字符串:删除括号额外的字母和数字的字符串

(aeiou 123) word one 

如何从括号,这样只有“字一个”遗体除去一切吗?

+0

发布您的尝试.. –

+0

@Raj提供的信息很少,范围没有定义 –

+0

@Raj:请提供您尝试的示例代码,但看起来像一个非常简单的问题。 – Dish

回答

1

你可以使用正则表达式,如:

String str = "(aeiou 123) word one"; 
str = str.replaceAll("\\([^\\)]*\\)", "").trim(); 
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没有正确配对(()

相关问题