Java String Split by Delimiter split(final String str, final char delim)

Here you can find the source of split(final String str, final char delim)

Description

split

License

Open Source License

Parameter

Parameter Description
str a parameter
delim a parameter

Return

String[]

Declaration

public static String[] split(final String str, final char delim) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester/* w  w  w .  ja  v  a  2  s. co  m*/
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2008 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.util.*;

public class Main {
    /**
     * 
     * @param str
     * @param delim
     * @return String[]
     */
    public static String[] split(final String str, final char delim) {
        final List<String> tokens = new ArrayList<>();
        final char[] charArray = str.toCharArray();
        final StringBuffer token = new StringBuffer();

        for (int i = 0; i < charArray.length; i++) {
            if (charArray[i] == delim) {
                tokens.add(token.toString());
                token.setLength(0);
            } else {
                token.append(charArray[i]);
            }
        }

        if (token.length() > 0) {
            tokens.add(token.toString());
        }

        return tokens.toArray(new String[tokens.size()]);
    }
}

Related

  1. fastSplit(String string, String delimiter)
  2. split(final boolean enable, final String value, final char delimiter)
  3. split(final String input, final char delimiter)
  4. split(final String input, final String delimiter, final boolean removeEmpty)
  5. split(final String src, final char delim)
  6. split(final String str, final String delimiter)
  7. split(String a, String delim)
  8. split(String input, char delimiter)
  9. split(String input, String delimiter)