Java String Split by Delimiter split(String src, char delim)

Here you can find the source of split(String src, char delim)

Description

A String#split(*) replacement that splits on the provided char.

License

Open Source License

Parameter

Parameter Description
src The string to be split
delim The character to split on

Return

An array containing the split. Might be empty, but will not be null.

Declaration

public static String[] split(String src, char delim) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2010 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
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w  ww  .j av  a  2s .c  o  m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.ArrayList;

public class Main {
    public final static String ZERO_LENGTH_STRING = "";
    public final static String[] EMPTY_STRING_ARRAY = new String[0];

    /**
     * A String#split(*) replacement that splits on the provided char. No Regex
     * involved.
     * 
     * @param src
     *            The string to be split
     * @param delim
     *            The character to split on
     * @return An array containing the split. Might be empty, but will not be
     *         <code>null</code>.
     */
    public static String[] split(String src, char delim) {
        if (src == null) {
            return EMPTY_STRING_ARRAY;
        }

        if (src.length() == 0) {
            return new String[] { ZERO_LENGTH_STRING };
        }

        ArrayList result = new ArrayList();
        int idx = src.indexOf(delim);
        int lastIdx = 0;
        while (idx != -1) {
            result.add(src.substring(lastIdx, idx));
            lastIdx = idx + 1;
            if (lastIdx == src.length()) {
                idx = -1;
            } else {
                idx = src.indexOf(delim, lastIdx);
            }
        }
        if (lastIdx < src.length()) {
            result.add(src.substring(lastIdx));
        }
        String[] resultArray = (String[]) result.toArray(new String[result.size()]);
        boolean allEmpty = true;
        for (int i = 0; i < resultArray.length && allEmpty; i++) {
            if (resultArray[i].length() > 0) {
                allEmpty = false;
            }
        }
        if (allEmpty) {
            return EMPTY_STRING_ARRAY;
        }
        return resultArray;
    }
}

Related

  1. split(String s, String delim)
  2. split(String s, String delim)
  3. split(String s, String delimiter)
  4. split(String s, String delimiter)
  5. split(String s, String delimiter)
  6. split(String src, char delim)
  7. split(String src, String delimiter)
  8. split(String str, char delim)
  9. split(String str, char delimiter)