Java String Split by Separator splitStaySeparator(String str, char token)

Here you can find the source of splitStaySeparator(String str, char token)

Description

split a String, if there are no character between two token this method place a empty String ( != String.split )

License

LGPL

Parameter

Parameter Description
str a standard str
token a token ( not pattern !!! )

Return

a array of String with the separator

Declaration

public static String[] splitStaySeparator(String str, char token) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;

public class Main {
    /**/*from   w w  w  .  j a  v a 2s. c om*/
      * split a String, if there are no character between two token this method place
      * a empty String ( != String.split )
      * 
      * @param str
      *            a standard str
      * @param token
      *            a token ( not pattern !!! )
      * @return a array of String with the separator
      */
    public static String[] splitStaySeparator(String str, char token) {
        int i = str.indexOf(token);
        int start = 0;
        ArrayList<String> arrayList = new ArrayList<String>();

        while (i >= 0) {
            arrayList.add(str.substring(start, i));
            start = i + 1;
            arrayList.add(str.substring(i, i + 1));
            i = str.indexOf(token, start);
        }

        arrayList.add(str.substring(start, str.length()));
        String[] res = new String[arrayList.size()];
        arrayList.toArray(res);
        return res;
    }

    /**
      * split a String, if there are no character between two token this method place
      * a empty String ( != String.split )
      * 
      * @param str
      *            a standard str
      * @param token
      *            a token ( not pattern !!! )
      * @return a array of String with the separator
      */
    public static String[] splitStaySeparator(String str, String token) {
        if ((token == null) || (token.length() == 0)) {
            return new String[] { str };
        }

        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add(str);

        char[] sep = token.toCharArray();
        for (char c : sep) {
            ArrayList<String> newArrayList = new ArrayList<String>();
            for (String string : arrayList) {
                String[] splitedString = splitStaySeparator(string, c);
                for (String string2 : splitedString) {
                    newArrayList.add(string2);
                }
            }
            arrayList = newArrayList;
        }

        String[] res = new String[arrayList.size()];
        arrayList.toArray(res);
        return res;
    }
}

Related

  1. splitInts(String string, String separator)
  2. splitList(String source, char separator)
  3. splitListBySeparator(String text, String separator)
  4. splitNotRegex(String str, String separatorChars)
  5. splitSmart(String s, char separator)
  6. splitString(String sInput, String sSeparator)
  7. splitStringToLong(String strInput, String separator)
  8. splitStringWithBracesOnSeparator(String string, char separator)
  9. splitStructs(String str, char separator)