Java String Split by Delimiter splitString(final String values, final char delimiter)

Here you can find the source of splitString(final String values, final char delimiter)

Description

split String

License

Open Source License

Declaration

public static String[] splitString(final String values, final char delimiter) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 * The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:// ww w  .j  ava  2s . com
 *     Jan S. Rellermeyer, IBM Research - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    private static String[] EMPTY_STRING_ARRAY = new String[0];

    public static String[] splitString(final String values, final char delimiter) {
        return splitString(values, delimiter, Integer.MAX_VALUE);
    }

    static String[] splitString(final String values, final char delimiter, final int limit) {
        if (values == null || values.length() == 0) {
            return EMPTY_STRING_ARRAY;
        }

        final List<String> tokens = new ArrayList<String>(values.length() / 10);

        final char[] chars = values.toCharArray();

        final int len = chars.length;
        int openingQuote = -1;
        int pointer = 0;
        int curr = 0;
        int matches = 0;

        // skip trailing whitespaces
        while (Character.isWhitespace(chars[curr])) {
            curr++;
        }

        pointer = curr;

        do {
            if (chars[curr] == '\\') {
                curr += 2;
                continue;
            } else if (chars[curr] == '"') {
                if (openingQuote < 0) {
                    openingQuote = curr;
                } else {
                    openingQuote = -1;
                }

                curr++;
                continue;
            } else if (chars[curr] == delimiter && openingQuote < 0) {
                matches++;
                if (matches > limit) {
                    break;
                }

                // scan back to skip whitepspaces
                int endPointer = curr - 1;
                while (endPointer > 0 && Character.isWhitespace(chars[endPointer])) {
                    endPointer--;
                }

                // copy from pointer to current - 1
                final int count = endPointer - pointer + 1;
                if (count > 0) {
                    tokens.add(new String(chars, pointer, count));
                }

                curr++;

                // scan forward to skip whitespaces
                while (curr < len && Character.isWhitespace(chars[curr])) {
                    curr++;
                }

                pointer = curr;
                continue;
            }

            curr++;
        } while (curr < len);

        if (openingQuote > -1) {
            throw new IllegalArgumentException("Unmatched quotation mark at position " + openingQuote);
        }

        // scan back to skip whitepspaces
        int endPointer = len - 1;
        while (endPointer > 0 && Character.isWhitespace(chars[endPointer])) {
            endPointer--;
        }

        final int count = endPointer - pointer + 1;
        if (count > 0) {
            tokens.add(new String(chars, pointer, count));
        }

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

Related

  1. splitSimple(String delimiter, String str)
  2. splitSimpleLimit(String str, String delimiter, int limitSize)
  3. splitSqlScript(String script, char delim, List statements)
  4. splitStr(String str, char delimiter, boolean trim)
  5. splitString(CharSequence s, char delim)
  6. splitString(String data, String delimiter)
  7. splitString(String originalString, String delimeter)
  8. splitString(String source, String delimiter)
  9. splitString(String splitStr, String delim)