Here you can find the source of split(String string, String delimiter)
Parameter | Description |
---|---|
string | a parameter |
delimiter | a parameter |
public static String[] split(String string, String delimiter)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 Weltevree Beheer BV, Remain Software & Industrial-TSI * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Wim Jongman - initial API and implementation *******************************************************************************/ import java.util.ArrayList; public class Main { /**//w w w . j av a 2s.c o m * Splits the given string by delimiter. * * @param string * @param delimiter * @return the unprocessed array */ public static String[] split(String string, String delimiter) { String[] result; if (string == null) { result = new String[0]; return result; } ArrayList list = new ArrayList(); do { int location = string.indexOf(delimiter); if (location < 0) break; list.add(string.substring(0, location).trim()); string = string.substring(location + 1); } while (true); string = string.trim(); if (string.length() > 0) list.add(string); result = (String[]) list.toArray(new String[list.size()]); // StringTokenizer tizer = new StringTokenizer(string, delimiter); // String[] result = new String[tizer.countTokens()]; // for (int i = 0; tizer.hasMoreElements(); i++) { // result[i] = tizer.nextToken(); // } return result; } }