Java String Split by Separator stringSplit(String string, int separator, int escape)

Here you can find the source of stringSplit(String string, int separator, int escape)

Description

string Split

License

Open Source License

Declaration

public static String[] stringSplit(String string, int separator, int escape) 

Method Source Code

//package com.java2s;
/*/*from w  w w . j a  v a2  s  .c  o  m*/
 This file belongs to the Servoy development and deployment environment, Copyright (C) 1997-2010 Servoy BV
    
 This program is free software; you can redistribute it and/or modify it under
 the terms of the GNU Affero General Public License as published by the Free
 Software Foundation; either version 3 of the License, or (at your option) any
 later version.
    
 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 Affero General Public License for more details.
    
 You should have received a copy of the GNU Affero General Public License along
 with this program; if not, see http://www.gnu.org/licenses or write to the Free
 Software Foundation,Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String[] stringSplit(String string, int separator, int escape) {
        int i = stringIndexOf(string, separator, escape);
        return i >= 0 ? new String[] { unescape(string.substring(0, i), escape), string.substring(i + 1) }
                : new String[] { unescape(string, escape), null };
    }

    public static String[] stringSplit(final String s, final String split) {
        if (s == null) {
            return new String[0];
        }
        final List<String> strings = new ArrayList<String>();
        int pos = 0;
        while (true) {
            int next = s.indexOf(split, pos);
            if (next == -1) {
                strings.add(s.substring(pos));
                break;
            } else {
                strings.add(s.substring(pos, next));
            }
            pos = next + 1;
        }
        final String[] result = new String[strings.size()];
        strings.toArray(result);
        return result;
    }

    public static int stringIndexOf(String string, int ch, int escape) {
        return stringIndexOf(string, ch, escape, 0);
    }

    public static int stringIndexOf(String string, int ch, int escape, int beginIndex) {
        int i = string.indexOf(ch, beginIndex);
        while (i > 0 && string.charAt(i - 1) == escape) {
            i = string.indexOf(ch, i + 1);
        }
        return i;
    }

    public static String unescape(String string, int escape) {
        StringBuilder buffer = new StringBuilder(string.length());
        int i = string.indexOf(escape), b = 0;
        while (i >= 0) {
            buffer.append(string.substring(b, i));
            if (i + 1 < string.length()) {
                buffer.append(string.charAt(i + 1));
            }
            b = i + 2;
            i = string.indexOf(string, b);
        }
        if (b < string.length()) {
            buffer.append(string.substring(b));
        }
        return buffer.toString();
    }
}

Related

  1. splitValues(String concatedValues, String separator)
  2. splitValues(String values, String separator)
  3. splitWithBreakingBrace(String str, char separator, char braceStart, char braceEnd)
  4. splitWithoutEscaped(String str, char separatorChar)
  5. splitWorker(final String str, final char separatorChar, final boolean preserveAllTokens)