Here you can find the source of splitAll(String str, char separatorChar)
Splits the provided text into an array, separator specified.
Parameter | Description |
---|---|
str | the String to parse, may be null |
separatorChar | the character used as the delimiter, <code>null</code> splits on whitespace |
public static String[] splitAll(String str, char separatorChar)
//package com.java2s; /**/* www . jav a 2s .c om*/ * Distribution License: * JSword is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License, version 2.1 as published by * the Free Software Foundation. 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 Lesser General Public License for more details. * * The License is available on the internet at: * http://www.gnu.org/copyleft/lgpl.html * or by writing to: * Free Software Foundation, Inc. * 59 Temple Place - Suite 330 * Boston, MA 02111-1307, USA * * Copyright: 2005 * The copyright to this program is held by it's authors. * * ID: $Id$ */ import java.util.ArrayList; import java.util.List; public class Main { /** * An empty immutable <code>String</code> array. */ public static final String[] EMPTY_STRING_ARRAY = new String[0]; /** * <p> * Splits the provided text into an array, separator specified. This is an * alternative to using StringTokenizer. * </p> * * <p> * The separator is not included in the returned String array. Adjacent * separators are treated individually. * </p> * * <p> * A <code>null</code> input String returns <code>null</code>. * </p> * * <pre> * StringUtils.split(null, *) = null * StringUtils.split("", *) = [] * StringUtils.split("a.b.c", '.') = ["a", "b", "c"] * StringUtils.split("a..b.c", '.') = ["a", "", "b", "c"] * StringUtils.split("a:b:c", '.') = ["a:b:c"] * StringUtils.split("a\tb\nc", null) = ["a", "b", "c"] * StringUtils.split("a b c", ' ') = ["a", "b", "c"] * </pre> * * @param str * the String to parse, may be null * @param separatorChar * the character used as the delimiter, <code>null</code> splits * on whitespace * @return an array of parsed Strings * @since 2.0 */ public static String[] splitAll(String str, char separatorChar) { // Performance tuned for 2.0 (JDK1.4) if (str == null) { return EMPTY_STRING_ARRAY.clone(); } int len = str.length(); if (len == 0) { return EMPTY_STRING_ARRAY.clone(); } List<String> list = new ArrayList<String>(); int i = 0; int start = 0; boolean match = false; while (i < len) { if (str.charAt(i) == separatorChar) { list.add(str.substring(start, i)); start = ++i; match = false; continue; } match = true; i++; } if (match) { list.add(str.substring(start, i)); } return list.toArray(new String[list.size()]); } /** * <p> * Splits the provided text into an array, separator specified. This is an * alternative to using StringTokenizer. * </p> * * <p> * The separator is not included in the returned String array. Adjacent * separators are treated individually. * </p> * * <p> * A <code>null</code> input String returns <code>null</code>. * </p> * * <pre> * StringUtils.split(null, *) = null * StringUtils.split("", *) = [] * StringUtils.split("a.b.c", '.') = ["a", "b", "c"] * StringUtils.split("a..b.c", '.') = ["a", "", "b", "c"] * StringUtils.split("a:b:c", '.') = ["a:b:c"] * StringUtils.split("a b c", ' ') = ["a", "b", "c"] * </pre> * * @param str * the String to parse, may be null * @param separatorChar * the character used as the delimiter * @param max * the maximum number of elements to include in the array. A zero * or negative value implies no limit * @return an array of parsed Strings * @since 2.0 */ public static String[] splitAll(String str, char separatorChar, int max) { // Performance tuned for 2.0 (JDK1.4) if (str == null) { return EMPTY_STRING_ARRAY.clone(); } int len = str.length(); if (len == 0) { return EMPTY_STRING_ARRAY.clone(); } List<String> list = new ArrayList<String>(); int sizePlus1 = 1; int i = 0; int start = 0; boolean match = false; while (i < len) { if (str.charAt(i) == separatorChar) { if (sizePlus1++ == max) { i = len; } list.add(str.substring(start, i)); start = ++i; match = false; continue; } match = true; i++; } if (match) { list.add(str.substring(start, i)); } return list.toArray(new String[list.size()]); } }