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

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

Description

split

License

Open Source License

Declaration

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

Method Source Code


//package com.java2s;
/*/*ww  w .j av  a2 s .  co  m*/
 * This file is part of libbluray
 * Copyright (C) 2010  Libbluray
 *
 * 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, see
 * <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

public class Main {
    public static String[] split(String str, char delimiter) {
        ArrayList elements = new ArrayList();
        int i, j = 0, len = str.length();
        for (i = 0; i < len; i++)
            if (str.charAt(i) == delimiter) {
                elements.add(str.substring(j, i));
                j = i + 1;
            }
        if (j < i)
            elements.add(str.substring(j, i));
        return (String[]) elements.toArray(new String[elements.size()]);
    }

    public static String[] split(String str, char delimiter, int num) {
        ArrayList elements = new ArrayList();
        int i, j = 0, n = 0, len = str.length();
        for (i = 0; (i < len) && (n < num); i++)
            if (str.charAt(i) == delimiter) {
                elements.add(str.substring(j, i));
                j = i + 1;
                n++;
            }
        if ((n < num) && (j < i)) {
            elements.add(str.substring(j, i));
            n++;
        }
        return (String[]) elements.toArray(new String[elements.size()]);
    }
}

Related

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