Java String Split by Delimiter split(final String str, final String delimiter)

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

Description

Split string into separate tokens.

License

BSD License

Parameter

Parameter Description
str A String
delimiter Token delimiter

Return

Array of tokens

Declaration

public static String[] split(final String str, final String delimiter) 

Method Source Code


//package com.java2s;
/*L/*from w w  w .  ja v a 2s  . co  m*/
 *  Copyright RTI International
 *
 *  Distributed under the OSI-approved BSD 3-Clause License.
 *  See http://ncip.github.com/webgenome/LICENSE.txt for details.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Split string into separate tokens.
     * @param str A String
     * @param delimiter Token delimiter
     * @return Array of tokens
     */
    public static String[] split(final String str, final String delimiter) {
        List<String> tokens = new ArrayList<String>();
        int p = 0;
        while (p < str.length()) {
            int q = str.indexOf(delimiter, p);
            if (q < 0) {
                q = str.length();
            }
            if (p == q) {
                tokens.add("");
            } else {
                tokens.add(str.substring(p, q).trim());
            }
            p = q + 1;
        }
        String[] tokenStr = new String[0];
        tokenStr = (String[]) tokens.toArray(tokenStr);
        return tokenStr;
    }
}

Related

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