Here you can find the source of splitQuotedString(String str)
static public List<String> splitQuotedString(String str)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { /** Splits a string based on spaces, grouping atoms if they are inside non escaped double quotes. *//* ww w.j a v a 2s.co m*/ static public List<String> splitQuotedString(String str) { List<String> strings = new ArrayList<String>(); boolean inQuotes = false; boolean quoteStateChange = false; StringBuffer buffer = new StringBuffer(); //Find some spaces, for (int i = 0; i < str.length(); i++) { //Have we toggled the quote state? char c = str.charAt(i); quoteStateChange = false; if (c == '"' && (i == 0 || str.charAt(i - 1) != '\\')) { inQuotes = !inQuotes; quoteStateChange = true; } //Peek at the next character - if we have a \", we need to only insert a " if (c == '\\' && i < str.length() - 1 && str.charAt(i + 1) == '"') { c = '"'; i++; } //If we're not in quotes, and we've hit a space... if (!inQuotes && str.charAt(i) == ' ') { //Do we actually have somthing in the buffer? if (buffer.length() > 0) { strings.add(buffer.toString()); buffer.setLength(0); } } else if (!quoteStateChange) { //We only want to add stuff to the buffer if we're forced to by quotes, or we're not a " buffer.append(c); } } //Add on the last string if needed if (buffer.length() > 0) { strings.add(buffer.toString()); } return strings; } }