Java String Split by Char splitInclusive(CharSequence input, char[] splitChars)

Here you can find the source of splitInclusive(CharSequence input, char[] splitChars)

Description

split Inclusive

License

Open Source License

Declaration

public static String[] splitInclusive(CharSequence input, char[] splitChars) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

public class Main {
    public static String[] splitInclusive(CharSequence input, char[] splitChars) {
        ArrayList<String> splited = new ArrayList<>();
        String readthusfar = "";

        for (int i = 0; i < input.length(); i++) {
            char readChar = input.charAt(i);

            if (containsChar(splitChars, readChar)) {
                if (!readthusfar.isEmpty()) {
                    splited.add(readthusfar);
                    readthusfar = "";
                }//from w w w. j a  v  a  2 s. c  o m

                splited.add(String.valueOf(readChar));

                continue;
            }

            readthusfar += readChar;
        }

        if (!readthusfar.isEmpty()) {
            splited.add(readthusfar);
        }

        String[] resultAsArray = new String[splited.size()];
        resultAsArray = splited.toArray(resultAsArray);

        return resultAsArray;
    }

    public static boolean containsChar(char[] array, char v) {
        for (char e : array)
            if (e == v)
                return true;

        return false;
    }
}

Related

  1. splitByChar(final String message, final char ch)
  2. splitByNonNestedChars(String s, char... c)
  3. splitChars(String input, String charsToBeRemoved)
  4. splitEncolosed(String s, char open_tag, char close_tag)
  5. splitFast3(String data, char splitChar)
  6. splitList(String source, char useChar)
  7. splitOnChar(String str, char c)
  8. splitOnChar(String str, int ch)
  9. splitOnCharArray(String value, String splitOn)