Here you can find the source of split(String source, String seperator)
Parameter | Description |
---|---|
source | The string that will be split into parts. |
seperator | The seperator string that will be used to determine the parts. |
ArrayList
containing the parts as String
objects.
public static ArrayList split(String source, String seperator)
//package com.java2s; /*/*from w w w.j a v a2 s . c om*/ * Copyright 2001-2006 Geert Bevin <gbevin[remove] at uwyn dot com> * Distributed under the terms of either: * - the common development and distribution license (CDDL), v1.0; or * - the GNU Lesser General Public License, v2.1 or later * $Id: StringUtils.java 3108 2006-03-13 18:03:00Z gbevin $ */ import java.util.ArrayList; public class Main { /** * Splits a string into different parts, using a seperator string to detect * the seperation boundaries in a case-sensitive manner. The seperator will * not be included in the list of parts. * * @param source The string that will be split into parts. * @param seperator The seperator string that will be used to determine the * parts. * * @return An <code>ArrayList</code> containing the parts as * <code>String</code> objects. * * @since 1.0 */ public static ArrayList split(String source, String seperator) { return split(source, seperator, true); } /** * Splits a string into different parts, using a seperator string to detect * the seperation boundaries. The seperator will not be included in the list * of parts. * * @param source The string that will be split into parts. * @param seperator The seperator string that will be used to determine the * parts. * @param matchCase A <code>boolean</code> indicating if the match is going * to be performed in a case-sensitive manner or not. * * @return An <code>ArrayList</code> containing the parts as * <code>String</code> objects. * * @since 1.0 */ public static ArrayList split(String source, String seperator, boolean matchCase) { ArrayList substrings = new ArrayList(); if (null == source) { return substrings; } if (null == seperator) { substrings.add(source); return substrings; } int current_index = 0; int delimiter_index = 0; String element = null; String source_lookup_reference = null; if (!matchCase) { source_lookup_reference = source.toLowerCase(); seperator = seperator.toLowerCase(); } else { source_lookup_reference = source; } while (current_index <= source_lookup_reference.length()) { delimiter_index = source_lookup_reference.indexOf(seperator, current_index); if (-1 == delimiter_index) { element = new String(source.substring(current_index, source.length())); substrings.add(element); current_index = source.length() + 1; } else { element = new String(source.substring(current_index, delimiter_index)); substrings.add(element); current_index = delimiter_index + seperator.length(); } } return substrings; } }