Java String Split by Quote split(String sString, boolean bIgnoreQuotes)

Here you can find the source of split(String sString, boolean bIgnoreQuotes)

Description

This is a simple utility to split strings.

License

Apache License

Parameter

Parameter Description
sString The string to split
bIgnoreQuotes For future implementation

Return

The splitted string

Declaration

public static String[] split(String sString, boolean bIgnoreQuotes) 

Method Source Code

//package com.java2s;
/*//from  w  ww  . j  a v  a  2  s. c om
 * Copyright (c) 2008 Massimiliano Ziccardi
 *  
 * 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;
import java.util.List;

public class Main {
    /**
     * This is a simple utility to split strings.
     * The string is splitted following these rules (in the order):
     * <ul>
     * <li>If a single quote (') or a double quote (") is found at the start of the word, the split will
     * occour at the next quote or double quote
     * <li>Otherwise, the split occurres as soon as a space is found.
     * </ul>
     * @param sString The string to split
     * @param bIgnoreQuotes For future implementation
     * @return The splitted string
     * 
     * @since JNRPE Server 1.04
     */
    public static String[] split(String sString, boolean bIgnoreQuotes) {
        List vRes = new ArrayList();
        char splitChar = ' ';
        char[] vChars = sString.trim().toCharArray();

        StringBuffer sbCurrentToken = new StringBuffer();

        for (int i = 0; i < vChars.length; i++) {
            if (vChars[i] == splitChar) {
                if (sbCurrentToken.toString().length() != 0) {
                    vRes.add(sbCurrentToken.toString());
                    sbCurrentToken = new StringBuffer();
                    splitChar = ' ';

                }
                continue;
            }

            switch (vChars[i]) {
            case '"':
            case '\'':
                if (sbCurrentToken.length() == 0 && splitChar == ' ') {
                    splitChar = vChars[i];
                    break;
                }
            default:
                sbCurrentToken.append(vChars[i]);
            }
        }

        if (sbCurrentToken.length() != 0)
            vRes.add(sbCurrentToken.toString());

        String[] vRet = new String[vRes.size()];

        for (int i = 0; i < vRet.length; i++)
            vRet[i] = (String) vRes.get(i);

        return vRet;
    }
}

Related

  1. quoteAwareSplit(String str, char delim)
  2. split(String str, char chrSplit, char chrQuote)
  3. splitArrayStringQuoted(String array, char quoteChar)
  4. splitHandleQuotes(String s)
  5. splitIgnoringQuotes(String str, char separatorChar)