que tenía un problema similar en la actualidad, y no se parece a ninguna opciones estándar tales como StringTokenizer, StrTokenizer, Sc anner fueron una buena opción. Sin embargo, no es difícil implementar lo básico.
Este ejemplo maneja todos los casos extremos actualmente comentados en otras respuestas. Tenga cuidado, todavía no lo he comprobado para ver si cumple con POSIX. Gist incluyendo pruebas unitarias disponibles on GitHub - lanzado en dominio público a través de la licencia no autorizada.
public List<String> parseArgs(CharSequence argString) {
List<String> tokens = new ArrayList<String>();
boolean escaping = false;
char quoteChar = ' ';
boolean quoting = false;
StringBuilder current = new StringBuilder() ;
for (int i = 0; i<argString.length(); i++) {
char c = argString.charAt(i);
if (escaping) {
current.append(c);
escaping = false;
} else if (c == '\\' && !(quoting && quoteChar == '\'')) {
escaping = true;
} else if (quoting && c == quoteChar) {
quoting = false;
} else if (!quoting && (c == '\'' || c == '"')) {
quoting = true;
quoteChar = c;
} else if (!quoting && Character.isWhitespace(c)) {
if (current.length() > 0) {
tokens.add(current.toString());
current = new StringBuilder();
}
} else {
current.append(c);
}
}
if (current.length() > 0) {
tokens.add(current.toString());
}
return tokens;
}
Cabe destacar que "como el caparazón los procesaría" es una tarea bastante difícil; 'shlex' lo hace bien, pero muchos algoritmos ingenuos no lo harán. Por ejemplo, en shell, '" tres cuatro "' y '" tres "'' cuatro 'son exactamente equivalentes, como lo es' three \ four'. –