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

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

Description

Method that splits a string with delimited fields into an array of strings.

License

Open Source License

Parameter

Parameter Description
str String with delimited fields.
delim String that represents the delimiter.

Return

the String[] that contains all splitted fields.

Declaration

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

Method Source Code

//package com.java2s;
/***/*from   w w  w .j av  a 2s .  co m*/
 * jwma Java WebMail
 * Copyright (c) 2000-2003 jwma team
 *
 * jwma is free software; you can distribute and use this source
 * under the terms of the BSD-style license received along with
 * the distribution.
 ***/

import java.util.*;

public class Main {
    /**
     * Method that splits a string with delimited fields
     * into an array of strings.
     *
     * @param str String with delimited fields.
     * @param delim String that represents the delimiter.
     *
     * @return the String[] that contains all splitted fields.
     */
    public static String[] split(String str, String delim) {

        StringTokenizer strtok = new StringTokenizer(str, delim);
        String[] result = new String[strtok.countTokens()];

        for (int i = 0; i < result.length; i++) {
            result[i] = strtok.nextToken();
        }

        return result;
    }
}

Related

  1. split(String str, char delimiter)
  2. split(String str, char delimiter)
  3. split(String str, char delimiter)
  4. split(String str, char delimiter, boolean trim)
  5. split(String str, int delim, String trailing)
  6. split(String str, String delim)
  7. split(String str, String delim)
  8. split(String str, String delim)
  9. split(String str, String delim)