Java String Split split(String s, String seperator)

Here you can find the source of split(String s, String seperator)

Description

custom string splitting function as CDC/Foundation does not include String.split();

License

Open Source License

Parameter

Parameter Description
s Input String
seperator a parameter

Declaration

public static String[] split(String s, String seperator) 

Method Source Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**//from  w ww  .j  a v a2  s.c  om
     * custom string splitting function as CDC/Foundation does not include
     * String.split();
     * 
     * @param s
     *            Input String
     * @param seperator
     * @return
     */
    public static String[] split(String s, String seperator) {
        if (s == null || seperator == null || s.length() == 0 || seperator.length() == 0) {
            return (new String[0]);
        }

        List tokens = new ArrayList();
        String token;
        int index_a = 0;
        int index_b = 0;

        while (true) {
            index_b = s.indexOf(seperator, index_a);
            if (index_b == -1) {
                token = s.substring(index_a);

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

                break;
            }
            token = s.substring(index_a, index_b);

            if (token.length() >= 0) {
                tokens.add(token);
            }
            index_a = index_b + seperator.length();
        }
        String[] str_array = new String[tokens.size()];
        for (int i = 0; i < str_array.length; i++) {
            str_array[i] = (String) (tokens.get(i));
        }
        return str_array;
    }
}

Related

  1. split(String s)
  2. split(String s)
  3. split(String s, int c)
  4. split(String s, String at)
  5. split(String s, String s1)
  6. split(String s, String spliter, boolean removeEmptyItem)
  7. split(String s, String splitStr)
  8. split(String s, String splitter)
  9. split(String self)