2013-03-07 213 views
-2

我的单词列表,我必须去掉括号如何删除括号内的字符串?

day[1.0,264.0] 
developers[1.0,264.0] 
does[1.0,264.0] 
employees[1.0,264.0] 
ex[1.0,264.0] 
experts[1.0,264.0] 
fil[1.0,264.0] 
from[1.0,264.0] 
gr[1.0,264.0] 

内的字符串列表,我应该得到

day 

developers 

does 
. 
. 
. 
. 

是这种做法是否正确?

String rep=day[1.0,264.0]; 
String replaced=rep.replace("[","]","1.0","2"); 

这种做法是正确的?

Pattern stopWords = Pattern.compile("\\b(?:i|[|]|1|2|3|...)\\b\\s*",Pattern.CASE_INSENSITIVE);  
Matcher matcher = stopWords.matcher("I would like to do a nice novel about nature AND people");  
String clean = matcher.replaceAll(""); 
+0

尝试自己知道哪些变异工作。 – bsiamionau 2013-03-07 20:01:50

回答

5

比其他人建议的方法稍简单一些。

String s = "day[1.0,264.0]"; 
String ofInterest2 = s.substring(0, s.indexOf("[")); 

会给你输出

day 
+0

+1简直令人印象深刻:)和一个很好的逻辑。 – Praveen 2014-03-03 13:59:07

1

使用String#replaceAll(regex, repl)

String rep="day[1.0,264.0]"; 
rep = rep.replaceAll("\\[.*]",""); 

正则表达式:\\[.*][是正则表达式世界特殊字符(元chacrater),你必须逃离这将反斜杠把它作为一个文字。 .*是您在B/W什么“[什么这里]”

1

就什么也没有取代他们

rep.replaceAll("\\[.*\\]", ""); 
1

由“[”只要你的记号化字符串,并获得第一部分。

StringTokenizer st = new StringTokenizer(str, "["); 
String part1 = st.nextToken(); 
0

这使得东西方括号后以及

 String rep="day[1.0,264.0]"; 
    int firstIndex = rep.indexOf('['); 
    int secondIndex = rep.indexOf(']'); 
    String news = rep.substring(0, firstIndex) + rep.substring(secondIndex+1,rep.length());