Here you can find the source of splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
Parameter | Description |
---|---|
s | The String to split |
splitChar | The character to split on |
quoteChar | The character to quote items with |
escapeChar | The character to escape the quoteChar with |
public static String[] splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
//package com.java2s; import java.util.*; public class Main { /**/*www .j a v a2 s . c om*/ * This function splits the String s into multiple Strings using the * splitChar. However, it provides a quoting facility: it is possible to * quote strings with the quoteChar. * If the quoteChar occurs within the quotedExpression, it must be prefaced * by the escapeChar * * @param s The String to split * @param splitChar The character to split on * @param quoteChar The character to quote items with * @param escapeChar The character to escape the quoteChar with * @return An array of Strings that s is split into */ public static String[] splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar) { List<String> result = new ArrayList<String>(); int i = 0; int length = s.length(); StringBuilder b = new StringBuilder(); while (i < length) { char curr = s.charAt(i); if (curr == splitChar) { // add last buffer if (b.length() > 0) { result.add(b.toString()); b = new StringBuilder(); } i++; } else if (curr == quoteChar) { // find next instance of quoteChar i++; while (i < length) { curr = s.charAt(i); // mrsmith: changed this condition from // if (curr == escapeChar) { if ((curr == escapeChar) && (i + 1 < length) && (s.charAt(i + 1) == quoteChar)) { b.append(s.charAt(i + 1)); i += 2; } else if (curr == quoteChar) { i++; break; // break this loop } else { b.append(s.charAt(i)); i++; } } } else { b.append(curr); i++; } } if (b.length() > 0) { result.add(b.toString()); } return result.toArray(new String[result.size()]); } }