Java String Split splitWithoutEscaped(String str, String sep)

Here you can find the source of splitWithoutEscaped(String str, String sep)

Description

split Without Escaped

License

Open Source License

Declaration

public static String[] splitWithoutEscaped(String str, String sep) 

Method Source Code


//package com.java2s;
/* /* w w w  . j  ava 2  s.co  m*/
 * =============================================================
 * Copyright (C) 2007-2011 Edgenius (http://www.edgenius.com)
 * =============================================================
 * License Information: http://www.edgenius.com/licensing/edgenius/2.0/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2.0
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * http://www.gnu.org/licenses/gpl.txt
 *  
 * ****************************************************************
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * @return
     */
    public static String[] splitWithoutEscaped(String str, String sep) {
        if (str == null || "".equals(str))
            return new String[] { "" };

        List<String> list = new ArrayList<String>();
        int end = indexSeparatorWithoutEscaped(str, sep);
        while (end != -1) {
            list.add(str.substring(0, end));
            str = str.substring(end + sep.length());
            end = indexSeparatorWithoutEscaped(str, sep);
        }
        list.add(str);

        return list.toArray(new String[list.size()]);
    }

    public static boolean equals(String str1, String str2) {
        return str1 == null ? str2 == null : str1.equals(str2);
    }

    /**
     * find char index of sep, but not start with  odd number '\' 
     */
    public static int indexSeparatorWithoutEscaped(String str, String sep) {
        int start = 0;
        int idx;
        boolean found = false;
        do {
            idx = indexOf(str, sep, start);
            if (idx == 0) {
                //line of start
                found = true;
                break;
            }
            if (idx != -1) {
                int count = 0;
                for (int reIdx = idx - 1; reIdx >= 0; reIdx--) {
                    if ((str.charAt(reIdx) != '\\')) {
                        break;
                    }
                    count++;
                }
                if (count % 2 == 0) {
                    found = true;
                    break;
                }
            }
            start = idx + 1;
        } while (idx != -1);

        return found ? idx : -1;
    }

    public static int indexOf(String str, String searchChar, int startPos) {
        if (isEmpty(str)) {
            return -1;
        }
        return str.indexOf(searchChar, startPos);
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }
}

Related

  1. splitTypeArguments(final String nestedTypes)
  2. splitTypes(String fullName)
  3. splitValues(String strValues)
  4. splitValues(String value)
  5. splitVersion(String version)
  6. splitWS(String s, boolean decode)
  7. splitws(String val)
  8. stringSplit(String s)
  9. stringSplit(String string, String tokens, boolean trimStrings)