Java String Split by Case splitString(String string, Character character, boolean encased)

Here you can find the source of splitString(String string, Character character, boolean encased)

Description

Splits the string wherever character is found.

License

Open Source License

Declaration

public static ArrayList<String> splitString(String string, Character character, boolean encased) 

Method Source Code


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

import java.util.ArrayList;

public class Main {
    /**//www  .  j  av  a  2  s .com
     * Splits the string wherever character is found.
     * If encased is true, then strings that are encased in character will have that character at the start.
     *
     * Example
     *     splitString("String 1@String 2@ String 3", '@', true) -> ArrayList<String> "String 1", "@String 2", " String 3" </String>
     * */
    public static ArrayList<String> splitString(String string, Character character, boolean encased) {
        ArrayList<String> arrayList = new ArrayList<String>();

        string += character;
        int start = 0;
        int end = string.indexOf(character);
        if (end == 0)
            end = string.indexOf(character, 1);

        if (end == -1) {
            arrayList.add(string);
        }

        while (end != -1) {
            String string_object = string.substring(start, end);

            if (!string_object.isEmpty())
                arrayList.add(string_object);

            start = end;
            end = string.indexOf(character, end + 1);

            if (encased && !string_object.isEmpty() && character.equals(string_object.charAt(0))) {
                start++;
            }
        }

        return arrayList;
    }

    public static ArrayList<String> splitString(String string, Character character) {
        return splitString(string, character, false);
    }

    public static ArrayList<String> splitString(String string, Character[] characters) {
        ArrayList<String> stringList = new ArrayList<String>();
        stringList.add(string);

        for (Character character : characters) {
            String[] stringArray = stringList.toArray(new String[0]);
            stringList = new ArrayList<String>();

            for (String str : stringArray) {
                ArrayList<String> arrayList = splitString(str, character, false);
                for (String arrayString : arrayList) {
                    stringList.add(arrayString);
                }
            }
        }

        return stringList;
    }

    public static ArrayList<String> splitString(String string, ArrayList<Character> characters) {
        return splitString(string, (Character[]) characters.toArray());
    }
}

Related

  1. splitCamelCase(final String in)
  2. splitCamelCase(final String s)
  3. splitCamelCase(String src)
  4. splitCamelCase(String string)
  5. splitOnUppercase(String input)
  6. splitWords(String text, boolean toLowerCase)