Here you can find the source of split(String toSplit, char splitChar, boolean trim)
Parameter | Description |
---|---|
toSplit | the <code>String</code> to split. |
splitChar | the delimiting character. |
trim | if <code>true</code>, the tokens resulting from the split will be trimed. |
String
s corresponding to the tokens that resulted from the split.
public static String[] split(String toSplit, char splitChar, boolean trim)
//package com.java2s; /**/*ww w .j a v a 2 s . co m*/ * This class holds various utility methods. * * @author Yanick Duchesne * <dl> * <dt><b>Copyright: </b> * <dd>Copyright © 2002-2007 <a * href="http://www.sapia-oss.org">Sapia Open Source Software </a>. All * Rights Reserved.</dd> * </dt> * <dt><b>License: </b> * <dd>Read the license.txt file of the jar or visit the <a * href="http://www.sapia-oss.org/license.html">license page </a> at the * Sapia OSS web site</dd> * </dt> * </dl> */ import java.util.ArrayList; import java.util.List; public class Main { /** * Splits the given string into parts delimited by the given "split" * character. * * @param toSplit * the <code>String</code> to split. * @param splitChar * the delimiting character. * @param trim * if <code>true</code>, the tokens resulting from the split will * be trimed. * @return an array of <code>String</code> s corresponding to the tokens * that resulted from the split. */ public static String[] split(String toSplit, char splitChar, boolean trim) { List tokens = new ArrayList(); StringBuffer token = new StringBuffer(); for (int i = 0; i < toSplit.length(); i++) { if (toSplit.charAt(i) == splitChar) { if (trim) { tokens.add(token.toString().trim()); } else { tokens.add(token.toString()); } token.delete(0, token.length()); } else { token.append(toSplit.charAt(i)); } } if (token.length() > 0) { if (trim) { tokens.add(token.toString().trim()); } else { tokens.add(token.toString()); } } return (String[]) tokens.toArray(new String[tokens.size()]); } }