Java String Split by Separator splitSmart(String s, char separator)

Here you can find the source of splitSmart(String s, char separator)

Description

Split a string based on a separator, but don't split if it's inside a string.

License

Apache License

Declaration

public static List<String> splitSmart(String s, char separator) 

Method Source Code

//package com.java2s;
/*//from  w  w w .j av  a 2  s . c  om
 * Copyright 2008-2009 the original ???(zyc@hasor.net).
 *
 * 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 {
    /**
     * Split a string based on a separator, but don't split if it's inside
     * a string.  Assume '\' escapes the next char both inside and
     * outside strings.
     */
    public static List<String> splitSmart(String s, char separator) {
        ArrayList<String> lst = new ArrayList<String>(4);
        int pos = 0, start = 0, end = s.length();
        char inString = 0;
        char ch = 0;
        while (pos < end) {
            char prevChar = ch;
            ch = s.charAt(pos++);
            if (ch == '\\') { // skip escaped chars
                pos++;
            } else if (inString != 0 && ch == inString) {
                inString = 0;
            } else if (ch == '\'' || ch == '"') {
                // If char is directly preceeded by a number or letter
                // then don't treat it as the start of a string.
                // Examples: 50" TV, or can't
                if (!Character.isLetterOrDigit(prevChar)) {
                    inString = ch;
                }
            } else if (ch == separator && inString == 0) {
                lst.add(s.substring(start, pos - 1));
                start = pos;
            }
        }
        if (start < end) {
            lst.add(s.substring(start, end));
        }
        /***
        if (SolrCore.log.isLoggable(Level.FINEST)) {
          SolrCore.log.trace("splitCommand=" + lst);
        }
         ***/
        return lst;
    }

    /** Splits a backslash escaped string on the separator.
     * <p>
     * Current backslash escaping supported:
     * <br> \n \t \r \b \f are escaped the same as a Java String
     * <br> Other characters following a backslash are produced verbatim (\c => c)
     *
     * @param s  the string to split
     * @param separator the separator to split on
     * @param decode decode backslash escaping
     */
    public static List<String> splitSmart(String s, String separator,
            boolean decode) {
        ArrayList<String> lst = new ArrayList<String>(2);
        StringBuilder sb = new StringBuilder();
        int pos = 0, end = s.length();
        while (pos < end) {
            if (s.startsWith(separator, pos)) {
                if (sb.length() > 0) {
                    lst.add(sb.toString());
                    sb = new StringBuilder();
                }
                pos += separator.length();
                continue;
            }
            char ch = s.charAt(pos++);
            if (ch == '\\') {
                if (!decode)
                    sb.append(ch);
                if (pos >= end)
                    break; // ERROR, or let it go?
                ch = s.charAt(pos++);
                if (decode) {
                    switch (ch) {
                    case 'n':
                        ch = '\n';
                        break;
                    case 't':
                        ch = '\t';
                        break;
                    case 'r':
                        ch = '\r';
                        break;
                    case 'b':
                        ch = '\b';
                        break;
                    case 'f':
                        ch = '\f';
                        break;
                    }
                }
            }
            sb.append(ch);
        }
        if (sb.length() > 0) {
            lst.add(sb.toString());
        }
        return lst;
    }
}

Related

  1. splitField(String fieldWithSeparator, String separator)
  2. splitInts(String string, String separator)
  3. splitList(String source, char separator)
  4. splitListBySeparator(String text, String separator)
  5. splitNotRegex(String str, String separatorChars)
  6. splitStaySeparator(String str, char token)
  7. splitString(String sInput, String sSeparator)
  8. splitStringToLong(String strInput, String separator)
  9. splitStringWithBracesOnSeparator(String string, char separator)