Here you can find the source of splitArguments(String string)
public static String[] splitArguments(String string)
//package com.java2s; import java.util.ArrayList; public class Main { /**/*from www . j a va2 s . co m*/ * Splits specified string in a manner which is similar to what is done * for command line arguments. Returns the list of ``command line * arguments''. * <p>Example: returns <code>{"one", "two", "three", " \"four\" ", " * 'five' "}</code> for input string: * <pre>one "two" 'three' " \"four\" " " 'five' "</pre> * <p>Note that escaped sequences such as "\n" and "\t" contained in * specified string are left as is. The reason for this is that specified * string may contain any whitespace character including '\n' and '\t', * provided that these characters are contained in a quoted argument. */ public static String[] splitArguments(String string) { ArrayList<String> list = new ArrayList<String>(); char quote = 0; StringBuilder arg = null; final int length = string.length(); for (int i = 0; i < length; ++i) { char c = string.charAt(i); if (Character.isWhitespace(c)) { if (quote != 0) { arg.append(c); } else { if (arg != null) { list.add(arg.toString()); arg = null; } } } else { if (arg == null) { arg = new StringBuilder(); switch (c) { case '\"': case '\'': quote = c; break; default: arg.append(c); } } else { if (c == quote) { int last = arg.length() - 1; if (last >= 0 && arg.charAt(last) == '\\') { arg.setCharAt(last, quote); } else { list.add(arg.toString()); arg = null; quote = 0; } } else { arg.append(c); } } } } if (arg != null) { list.add(arg.toString()); } String[] args = new String[list.size()]; return list.toArray(args); } }