Java String Split by Delimiter split(String input, char delimiter)

Here you can find the source of split(String input, char delimiter)

Description

Splits a string into an array using a provided delimiter.

License

Apache License

Parameter

Parameter Description
input string to split.
delimiter delimiter

Return

a string into an array using a provided delimiter

Declaration

public static String[] split(String input, char delimiter) 

Method Source Code

//package com.java2s;
/*// w  w  w .j ava 2  s  .  c om
Copyright 2009-2014 Igor Polevoy
    
Licensed under the Apache License, Version 2.0 (the "License"); 
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 
    
http://www.apache.org/licenses/LICENSE-2.0 
    
Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License. 
*/

import java.util.*;

public class Main {
    /**
     * Splits a string into an array using a provided delimiter. The split chunks are also trimmed.
     *
     * @param input string to split.
     * @param delimiter  delimiter
     * @return a string into an array using a provided delimiter
     */
    public static String[] split(String input, char delimiter) {
        if (input == null)
            throw new NullPointerException("input cannot be null");

        List<String> tokens = new ArrayList<String>();
        StringTokenizer st = new StringTokenizer(input, new String(new byte[] { (byte) delimiter }));
        while (st.hasMoreTokens()) {
            tokens.add(st.nextToken().trim());
        }
        return tokens.toArray(new String[tokens.size()]);
    }
}

Related

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