Here you can find the source of splitQuoted(String input, Character split)
Parameter | Description |
---|---|
input | string to split |
split | split char |
public static String[] splitQuoted(String input, Character split)
//package com.java2s; /*//w w w . j a v a 2s. c o m * Copyright (c) 2016 Alexander Gr?n * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ import java.util.ArrayList; import java.util.List; public class Main { /** * Splits a string and returns the parts as array. * * @param input string to split * @param split split char * @return array of parts * @see #splitQuoted(String, Character, List) */ public static String[] splitQuoted(String input, Character split) { List<String> parts = new ArrayList<>(); splitQuoted(input, split, parts); String[] result = new String[parts.size()]; return parts.toArray(result); } /** * Split an input string at a specified split-character into several parts. * <tt>"</tt> and <tt>'</tt> are considered during the process. * <p><code>"testA testB testB" -> [testA,testB,testC]</code></p> * <p><code>"'testA testB' testB" -> [testA testB,testC]</code></p> * * @param input input string * @param split char used to split * @param parts list filled with the resulting parts */ @SuppressWarnings("ConstantConditions") public static void splitQuoted(String input, Character split, List<String> parts) { boolean inQuotation = false; boolean inDoubleQuotation = false; boolean escapeNext = false; int currentStart = 0; char[] chars = input.trim().toCharArray(); char c; String p; for (int i = 0; i < chars.length; i++) { c = chars[i]; boolean escape = escapeNext; escapeNext = false; if (!escape && c == '\\') { escapeNext = true; } else if (c == '\'') { if (inQuotation && !escape) { inQuotation = false; continue; } if (!inDoubleQuotation) { inQuotation = true; } } else if (c == '\"') { if (inDoubleQuotation && !escape) { inDoubleQuotation = false; continue; } if (!inDoubleQuotation) { inDoubleQuotation = true; } } else if (c == split && !inDoubleQuotation && !inQuotation) { int length = i - currentStart; if (length == 0) { p = ""; } else { p = input.substring(currentStart, currentStart + length); } parts.add(p); currentStart = i + 1; } } if (currentStart < chars.length) { p = input.substring(currentStart); parts.add(p); } } }