Here you can find the source of splitWithQuotes(String s)
Parameter | Description |
---|---|
s | The string to tokenize |
public static List<String> splitWithQuotes(String s)
//package com.java2s; import java.util.*; public class Main { /**//ww w . j ava2 s . com * tokenize the given string on spaces. Respect double quotes * * @param s The string to tokenize * @return the list of tokens */ public static List<String> splitWithQuotes(String s) { ArrayList<String> list = new ArrayList(); if (s == null) { return list; } // System.err.println ("S:" + s); while (true) { s = s.trim(); int qidx1 = s.indexOf("\""); int qidx2 = s.indexOf("\"", qidx1 + 1); int sidx1 = 0; int sidx2 = s.indexOf(" ", sidx1 + 1); if ((qidx1 < 0) && (sidx2 < 0)) { if (s.length() > 0) { list.add(s); } break; } if ((qidx1 >= 0) && ((sidx2 == -1) || (qidx1 < sidx2))) { if (qidx1 >= qidx2) { //Malformed string. Add the rest of the line and break if (qidx1 == 0) { s = s.substring(qidx1 + 1); } else if (qidx1 > 0) { s = s.substring(0, qidx1); } if (s.length() > 0) { list.add(s); } break; } if (qidx2 < 0) { //Malformed string. Add the rest of the line and break s = s.substring(1); list.add(s); break; } String tok = s.substring(qidx1 + 1, qidx2); if (tok.length() > 0) { list.add(tok); } s = s.substring(qidx2 + 1); // System.err.println ("qtok:" + tok); } else { if (sidx2 < 0) { list.add(s); break; } String tok = s.substring(sidx1, sidx2); if (tok.length() > 0) { list.add(tok); } s = s.substring(sidx2); // System.err.println ("stok:" + tok); } } return list; } }