Here you can find the source of split(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[] split(String str, char separatorChar)
//package com.java2s; /**/*from w ww . j ava 2 s .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, using whitespace as the * separator. Whitespace is defined by {@link Character#isWhitespace(char)}. * </p> * * <p> * The separator is not included in the returned String array. Adjacent * separators are treated as one separator. * </p> * * <p> * A <code>null</code> input String returns <code>null</code>. * </p> * * <pre> * StringUtils.split(null) = null * StringUtils.split("") = [] * StringUtils.split("abc def") = ["abc", "def"] * StringUtils.split("abc def") = ["abc", "def"] * StringUtils.split(" abc ") = ["abc"] * </pre> * * @param str * the String to parse, may be null * @return an array of parsed Strings, <code>null</code> if null String * input */ public static String[] split(String str) { return split(str, null, -1); } /** * <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 as one separator. * </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[] split(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) { if (match) { list.add(str.substring(start, i)); match = false; } start = ++i; 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, separators specified. This is an * alternative to using StringTokenizer. * </p> * * <p> * The separator is not included in the returned String array. Adjacent * separators are treated as one separator. * </p> * * <p> * A <code>null</code> input String returns <code>null</code>. A * <code>null</code> separatorChars splits on whitespace. * </p> * * <pre> * StringUtils.split(null, *) = null * StringUtils.split("", *) = [] * StringUtils.split("abc def", null) = ["abc", "def"] * StringUtils.split("abc def", " ") = ["abc", "def"] * StringUtils.split("abc def", " ") = ["abc", "def"] * StringUtils.split("ab:cd:ef", ":") = ["ab", "cd", "ef"] * </pre> * * @param str * the String to parse, may be null * @param separatorChars * the characters used as the delimiters, <code>null</code> * splits on whitespace * @return an array of parsed Strings, <code>null</code> if null String * input */ public static String[] split(String str, String separatorChars) { return split(str, separatorChars, -1); } /** * <p> * Splits the provided text into an array, separators specified. This is an * alternative to using StringTokenizer. * </p> * * <p> * The separator is not included in the returned String array. Adjacent * separators are treated as one separator. * </p> * * <p> * A <code>null</code> input String returns <code>null</code>. A * <code>null</code> separatorChars splits on whitespace. * </p> * * <pre> * StringUtils.split(null, *, *) = null * StringUtils.split("", *, *) = [] * StringUtils.split("ab de fg", null, 0) = ["ab", "cd", "ef"] * StringUtils.split("ab de fg", null, 0) = ["ab", "cd", "ef"] * StringUtils.split("ab:cd:ef", ":", 0) = ["ab", "cd", "ef"] * StringUtils.split("ab:cd:ef", ":", 2) = ["ab", "cd:ef"] * </pre> * * @param str * the String to parse, may be null * @param separatorChars * the characters used as the delimiters, <code>null</code> * splits on whitespace * @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 */ public static String[] split(String str, String separatorChars, int max) { // Performance tuned for 2.0 (JDK1.4) // Direct code is quicker than StringTokenizer. // Also, StringTokenizer uses isSpace() not isWhitespace() 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; if (separatorChars == null) { // Null separator means use whitespace while (i < len) { if (Character.isWhitespace(str.charAt(i))) { if (match) { if (sizePlus1++ == max) { i = len; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } match = true; i++; } } else if (separatorChars.length() == 1) { // Optimize 1 character case char sep = separatorChars.charAt(0); while (i < len) { if (str.charAt(i) == sep) { if (match) { if (sizePlus1++ == max) { i = len; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } match = true; i++; } } else { // standard case while (i < len) { if (separatorChars.indexOf(str.charAt(i)) >= 0) { if (match) { if (sizePlus1++ == max) { i = len; } list.add(str.substring(start, i)); match = false; } start = ++i; continue; } match = true; i++; } } if (match) { list.add(str.substring(start, i)); } return list.toArray(new String[list.size()]); } }