Java String Split split(String input)

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

Description

Splits an input string into a an array of strings, seperating at commas.

License

Apache License

Parameter

Parameter Description
input the string to split, possibly null or empty

Return

an array of the strings split at commas

Declaration

public static String[] split(String input) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

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

public class Main {
    /**/*from   w  w  w.  j  a  v  a2 s .co m*/
     * Splits an input string into a an array of strings, seperating
     * at commas.
     * 
     * @param input the string to split, possibly null or empty
     * @return an array of the strings split at commas
     */
    public static String[] split(String input) {
        return split(',', input);
    }

    public static String[] split(final char delimiter, final String input) {
        if (input == null)
            return new String[0];

        List<String> strings = new ArrayList<String>();

        int startx = 0;
        int cursor = 0;
        int length = input.length();

        while (cursor < length) {
            if (input.charAt(cursor) == delimiter) {
                String item = input.substring(startx, cursor);
                strings.add(item);
                startx = cursor + 1;
            }

            cursor++;
        }

        if (startx < length)
            strings.add(input.substring(startx));

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

Related

  1. split(String arg)
  2. split(String candidate)
  3. split(String cmd)
  4. split(String content)
  5. Split(String content, String sub_seq)
  6. split(String input)
  7. split(String input)
  8. split(String s)
  9. split(String s)