QuotedStringTokenizer extends StringTokenizer { public static String quoteIfNeeded(String s, String delim) { if (s == null) return null; if (s.length() == 0) return "\"\""; for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c == '\\' || c == '"' || c == '\'' || Character.isWhitespace(c) || delim.indexOf(c) >= 0) { StringBuilder b = new StringBuilder(s.length() + 8); quote(b, s); return b.toString(); } } return s; } QuotedStringTokenizer(String str, String delimiter, boolean returnDelimiters, boolean returnQuotes);  QuotedStringTokenizer(String str, String delimiter, boolean returnDelimiters);  QuotedStringTokenizer(String str, String delimiter);  QuotedStringTokenizer(String str); static String quoteIfNeeded(String s, String delim); static String quote(String s); static void quoteOnly(Appendable buffer, String input); static void quote(Appendable buffer, String input); static String unquoteOnly(String s); static String unquoteOnly(String s, boolean lenient); static String unquote(String s); static String unquote(String s, boolean lenient); static boolean isQuoted(String s); @Override boolean hasMoreTokens(); @Override String nextToken(); @Override String nextToken(String delim); @Override boolean hasMoreElements(); @Override Object nextElement(); @Override int countTokens(); boolean getDouble(); void setDouble(boolean d); boolean getSingle(); void setSingle(boolean single);  }
@Test void testQuoteIfNeeded() { assertEquals("abc", QuotedStringTokenizer.quoteIfNeeded("abc", " ,")); assertEquals("\"a c\"", QuotedStringTokenizer.quoteIfNeeded("a c", " ,")); assertEquals("\"a'c\"", QuotedStringTokenizer.quoteIfNeeded("a'c", " ,")); assertEquals("\"a\\n\\r\\t\"", QuotedStringTokenizer.quote("a\n\r\t")); assertEquals("\"\\u0000\\u001f\"", QuotedStringTokenizer.quote("\u0000\u001f")); }