2014-03-25 33 views
-1

我需要標記這個字符串。如何在java中使用split()標記字符串?

AddItem rt456 4 12 BOOK "File Structures" "Addison-Wesley" "Michael Folk" 

我需要

"AddItem","rt456","12","BOOK","File Structures","Addison-Wesley","Michael Folk" 

子。我如何使用split()獲得這些標記?

+0

真的,'split'?爲什麼你想讓你的生活複雜化?不會簡單模式/匹配器更簡單嗎?您也可以爲此編寫自己的簡單解析器。 – Pshemo

回答

0
List<String> list = new ArrayList<String>(); 
Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(str); 
while (m.find()) 
    list.add(m.group(1)); 

輸出:

[AddItem, rt456, 4, 12, BOOK, "File Structures", "Addison-Wesley", "Michael Folk"] 
+0

@ user3460845:使用'replaceAll(「\」「,」「);' – Brian

相關問題