2013-03-07 250 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()); 
相關問題