Java String Split by Delimiter split(String str, char delimiter)

Here you can find the source of split(String str, char delimiter)

Description

Split a string based on the given delimiter, but don't remove empty elements.

License

Open Source License

Parameter

Parameter Description
str The string to be split.
delimiter Split string based on this delimiter.

Return

Array of split strings. Guaranteeded to be not null.

Declaration

public static String[] split(String str, char delimiter) 

Method Source Code


//package com.java2s;
/*//from  w ww  .  ja  v a2s.com
 * Copyright (C) 2001-2003 Colin Bell
 * colbell@users.sourceforge.net
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

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

public class Main {
    /**
     * Split a string based on the given delimiter, but don't remove
     * empty elements.
     *
     * @param   str         The string to be split.
     * @param   delimiter   Split string based on this delimiter.
     *
     * @return   Array of split strings. Guaranteeded to be not null.
     */
    public static String[] split(String str, char delimiter) {
        return split(str, delimiter, false);
    }

    /**
     * Split a string based on the given delimiter, optionally removing
     * empty elements.
     *
     * @param   str         The string to be split.
     * @param   delimiter   Split string based on this delimiter.
     * @param   removeEmpty   If <tt>true</tt> then remove empty elements.
     *
     * @return   Array of split strings. Guaranteeded to be not null.
     */
    public static String[] split(String str, char delimiter, boolean removeEmpty) {
        // Return empty list if source string is empty.
        final int len = (str == null) ? 0 : str.length();
        if (len == 0) {
            return new String[0];
        }

        final List<String> result = new ArrayList<String>();
        String elem = null;
        int i = 0, j = 0;
        while (j != -1 && j < len) {
            j = str.indexOf(delimiter, i);
            elem = (j != -1) ? str.substring(i, j) : str.substring(i);
            i = j + 1;
            if (!removeEmpty || !(elem == null || elem.length() == 0)) {
                result.add(elem);
            }
        }
        return result.toArray(new String[result.size()]);
    }
}

Related

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