Java String Split by Delimiter split(String str, String delimiter, int limit)

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

Description

split

License

Open Source License

Declaration

public static String[] split(String str, String delimiter, int limit) 

Method Source Code


//package com.java2s;
/*//w  w  w.ja  va2  s.c  om
 * Copyright (C) 2014 Vincent Quatrevieux <quatrevieux.vincent@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

public class Main {

    public static String[] split(String str, String delimiter, int limit) {
        ArrayList<String> splited = new ArrayList<String>();

        int last = 0, pos, step = 0;

        if (limit < 1) {
            limit = Integer.MAX_VALUE; //devrait suffire amplement x)
        }

        while ((pos = str.indexOf(delimiter, last)) != -1 && ++step < limit) {
            splited.add(str.substring(last, pos));
            last = pos + 1;
        }

        splited.add(str.substring(last));

        String[] ret = new String[splited.size()];
        return splited.toArray(ret);
    }

    public static String[] split(String str, String delimiter) {
        return split(str, delimiter, 0);
    }
}

Related

  1. split(String str, String delimiter)
  2. split(String str, String delimiter)
  3. split(String str, String delimiter)
  4. split(String str, String delimiter)
  5. split(String str, String delimiter)
  6. split(String str, String delims)
  7. split(String str, String regexDelim, int limitUseRegex)
  8. split(String string, String delim, boolean doTrim)
  9. split(String string, String delimiter)