Here you can find the source of splitParams(String string, int max)
public static String[] splitParams(String string, int max)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; public class Main { public static String[] splitParams(String string, int max) { String[] words = string.trim().split(" "); if (words.length <= 1) { return words; }/*from w w w. j a v a2 s . c o m*/ ArrayList<String> list = new ArrayList<String>(); char quote = ' '; String building = ""; for (String word : words) { if (word.length() == 0) continue; if (max > 0 && list.size() == max - 1) { if (!building.isEmpty()) building += " "; building += word; } else if (quote == ' ') { if (word.length() == 1 || (word.charAt(0) != '"' && word.charAt(0) != '\'')) { list.add(word); } else { quote = word.charAt(0); if (quote == word.charAt(word.length() - 1)) { quote = ' '; list.add(word.substring(1, word.length() - 1)); } else { building = word.substring(1); } } } else { if (word.charAt(word.length() - 1) == quote) { list.add(building + " " + word.substring(0, word.length() - 1)); building = ""; quote = ' '; } else { building += " " + word; } } } if (!building.isEmpty()) { list.add(building); } return list.toArray(new String[list.size()]); } public static String[] splitParams(String string) { return splitParams(string, 0); } public static String[] splitParams(String[] split, int max) { return splitParams(arrayJoin(split, ' '), max); } public static String[] splitParams(String[] split) { return splitParams(arrayJoin(split, ' '), 0); } public static String arrayJoin(String[] array, char with) { if (array == null || array.length == 0) { return ""; } int len = array.length; StringBuilder sb = new StringBuilder(16 + len * 8); sb.append(array[0]); for (int i = 1; i < len; i++) { sb.append(with); sb.append(array[i]); } return sb.toString(); } }