Java String Split by Quote splitIgoringQuotes(String str)

Here you can find the source of splitIgoringQuotes(String str)

Description

Split method will split the given string to substrings every time it finds the space character (' ').

License

Apache License

Parameter

Parameter Description
str the string to be split

Return

a newly allocated String[] containing each word inside the string.

Declaration

public static String[] splitIgoringQuotes(String str) 

Method Source Code


//package com.java2s;
/*/* ww  w . jav  a 2s.  c  om*/
 *    Copyright [2016] [Thanasis Argyroudis]
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
 http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

import java.util.ArrayList;

public class Main {
    /**
     * Split method will split the given string to substrings every time it finds the
     * space character (' '). If space found inside quotes it will be ignored and
     * therefore not split will happen on that space.
     * <p>
     * This class will <b>trim</b> the sting.
     * <p>
     * Example: <blockquote>
     * 
     * <pre>
     * "  This is       a   \"Example    String\"   !   \"Test1 \"  "
     * 
     * Will yield: [This] [is] [a] [Example    String] [!] [Test1 ]
     * </pre>
     * 
     * </blockquote>
     * 
     * @param str
     *            the string to be split
     * 
     * @return a newly allocated String[] containing each word inside the string.
     */
    public static String[] splitIgoringQuotes(String str) {
        ArrayList<String> strings = new ArrayList<String>(5);
        StringBuilder sb = new StringBuilder();
        str = str.trim();

        try {
            for (int i = 0; i < str.length(); i++) {

                while (str.charAt(i) == ' ') //Skip spaces that may exist between.
                    i++;

                if (str.charAt(i) == '"') {
                    i++;
                    while (str.charAt(i) != '"')
                        sb.append(str.charAt(i++));
                } else {
                    while (str.charAt(i) != ' ')
                        sb.append(str.charAt(i++));
                }

                strings.add(sb.toString());
                sb.setLength(0); //Clear string builder
            }
        } catch (IndexOutOfBoundsException e) { //Easy way to detect if string end
            strings.add(sb.toString());
        } catch (NullPointerException nullString) {
            throw nullString;
        }
        return strings.toArray(new String[0]); //Passing 0-size string for performance reasons.
    }
}

Related

  1. split(String sString, boolean bIgnoreQuotes)
  2. split(String str, char chrSplit, char chrQuote)
  3. splitArrayStringQuoted(String array, char quoteChar)
  4. splitHandleQuotes(String s)
  5. splitIgnoringQuotes(String str, char separatorChar)
  6. splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
  7. splitOnCharWithQuoting(String s, char splitChar, char quoteChar, char escapeChar)
  8. splitOptions(String quotedOptionString)
  9. splitQuoted(String input, Character split)