Java String Split by Char split(String toSplit, char splitChar, boolean trim)

Here you can find the source of split(String toSplit, char splitChar, boolean trim)

Description

Splits the given string into parts delimited by the given "split" character.

License

Open Source License

Parameter

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.

Return

an array of String s corresponding to the tokens that resulted from the split.

Declaration

public static String[] split(String toSplit, char splitChar, boolean trim) 

Method Source Code

//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 &#169; 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()]);
    }
}

Related

  1. split(String str, char splitChar)
  2. split(String str, char splitChar)
  3. Split(String Str, char splitchar)
  4. split(String string, char c)
  5. split(String string, char character)
  6. split(String val, char ch)
  7. split(String value, char splitChar)
  8. splitAt(String inputString, Character inputChar)
  9. splitAt(String str, char c)