Java中的空格拆分string,除非在引号之间(即把“hello world”当作一个标记)
如何根据空间拆分String ,但将引用的子string作为一个单词? 
例:
 Location "Welcome to india" Bangalore Channai "IT city" Mysore 
 它应该被存储在ArrayList中 
 Location Welcome to india Bangalore Channai IT city Mysore 
	
就是这样:
 String str = "Location \"Welcome to india\" Bangalore " + "Channai \"IT city\" Mysore"; List<String> list = new ArrayList<String>(); Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(str); while (m.find()) list.add(m.group(1)); // Add .replace("\"", "") to remove surrounding quotes. System.out.println(list); 
输出:
 [Location, "Welcome to india", Bangalore, Channai, "IT city", Mysore] 
正则expression式只是说
-   [^"]– 以非"
-   \S*– 后跟零个或多个非空格字符
- …要么…
-  ".+?"– 一个"符号后面是什么,直到另一个"。
首先分开双引号:
 String s = 'Location "Welcome to india" Bangalore Channai "IT city" Mysore'; String[] splitted = s.split('"'); 
然后使用空格分割数组中的每个string
 for(int i = 0; i< splitted.length; i++){ //split each splitted cell and store in your arraylist }