Here you can find the source of splitValues(String strValues)
public static String[] splitValues(String strValues)
//package com.java2s; /**//from w w w . j av a 2 s . c om * Copyright (c) 2011, 2014 Eurotech and/or its affiliates * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Eurotech */ import java.util.ArrayList; import java.util.List; public class Main { private static final char DELIMITER = ','; private static final char ESCAPE = '\\'; public static String[] splitValues(String strValues) { // List<String> defaultValues = new ArrayList<String>(); // String[] stringValues = defaultValue.split("(?<!\\\\)(?>\\\\\\\\)*,"); // for (int i=0; i<stringValues.length; i++) { // if (stringValues[i] != null && stringValues[i].trim().length() > 0) { // defaultValues.add(unescapeString(stringValues[i])); // } // } // return defaultValues.toArray( new String[]{}); if (strValues == null) return null; // The trick is to strip out unescaped whitespace characters before and // after the input string as well as before and after each // individual token within the input string without losing any escaped // whitespace characters. Whitespace between two non-whitespace // characters may or may not be escaped. Also, any character may be // escaped. The escape character is '\'. The delimiter is ','. List<String> values = new ArrayList<String>(); StringBuilder buffer = new StringBuilder(); // Loop over the characters within the input string and extract each // value token. for (int i = 0; i < strValues.length(); i++) { char c1 = strValues.charAt(i); switch (c1) { case DELIMITER: // When the delimiter is encountered, add the extracted // token to the result and prepare the buffer to receive the // next token. values.add(buffer.toString()); buffer.delete(0, buffer.length()); break; case ESCAPE: // When the escape is encountered, add the immediately // following character to the token, unless the end of the // input has been reached. Note this will result in loop // counter 'i' being incremented twice, once here and once // at the end of the loop. if (i + 1 < strValues.length()) { buffer.append(strValues.charAt(++i)); } // If the ESCAPE character occurs as the last character // of the string, ignore it. break; default: // For all other characters, add them to the current token // unless dealing with unescaped whitespace at the beginning // or end. We know the whitespace is unescaped because it // would have been handled in the ESCAPE case otherwise. if (Character.isWhitespace(c1)) { // Ignore unescaped whitespace at the beginning of the // token. if (buffer.length() == 0) { continue; } // If the whitespace is not at the beginning, look // forward, starting with the next character, to see if // it's in the middle or at the end. Unescaped // whitespace in the middle is okay. for (int j = i + 1; j < strValues.length(); j++) { // Keep looping until the end of the string is // reached or a non-whitespace character other than // the escape is seen. char c2 = strValues.charAt(j); if (!Character.isWhitespace(c2)) { // If the current character is not the DELIMITER, all whitespace // characters are significant and should be added to the token. // Otherwise, they're at the end and should be ignored. But watch // out for an escape character at the end of the input. Ignore it // and any previous insignificant whitespace if it exists. if (c2 == ESCAPE && j + 1 >= strValues.length()) { continue; } if (c2 != DELIMITER) { buffer.append(strValues.substring(i, j)); } // Let loop counter i catch up with the inner loop but keep in // mind it will still be incremented at the end of the outer loop. i = j - 1; break; } } } else { // For non-whitespace characters. buffer.append(c1); } } } // Don't forget to add the last token. values.add(buffer.toString()); return values.toArray(new String[] {}); } }