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

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

Description

Split a string on a delimiter.

License

Open Source License

Parameter

Parameter Description
str The string to split.
delim The delimiter to split on.

Return

The substrings of str that were seperated by delim.

Declaration

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

Method Source Code


//package com.java2s;
/*//from   w  w  w  .ja  v a 2  s  .  c  om
 *  Copyright (C) 2012 Ed Schaller <schallee@darkmist.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 {
    private static final String[] EMPTY_STRING_ARRAY = new String[0];

    /**
     * Split a string on a delimiter.
     * @param str The string to split.
     * @param delim The delimiter to split on.
     * @return The substrings of str that were seperated by delim.
     */
    public static String[] split(String str, char delim) {
        List<String> strs;
        int len;
        int start, end;

        if (str == null)
            return EMPTY_STRING_ARRAY;
        if ((len = str.length()) == 0)
            return new String[] { "" };
        strs = new ArrayList<String>(len);
        for (start = 0; start < len && (end = str.indexOf(delim, start)) >= 0; start = end + 1)
            strs.add(str.substring(start, end));
        strs.add(str.substring(start));
        return strs.toArray(EMPTY_STRING_ARRAY);
    }
}

Related

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