Java String Split splitOn(String toSplit, String splitter)

Here you can find the source of splitOn(String toSplit, String splitter)

Description

Splitters

License

Apache License

Declaration


public static List<String> splitOn(String toSplit, String splitter) 

Method Source Code


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

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

public class Main {
    /**********************************************
     *  Splitters//from   w w w  .j  a  v  a2  s .c o  m
     ***********************************************/

    public static List<String> splitOn(String toSplit, String splitter) {
        List<String> result = new ArrayList<String>();

        //don't use String's .split, it uses regexes!

        String processedToSplit = toSplit;
        while (processedToSplit.contains(splitter)) {
            int firstOccurance = processedToSplit.indexOf(splitter);
            result.add(processedToSplit.substring(0, firstOccurance));
            processedToSplit = processedToSplit.substring(firstOccurance + splitter.length());
        }
        result.add(processedToSplit);

        result = concatElementsWithBrackets(result);

        return result;
    }

    public static List<String> splitOn(List<String> toSplit, String splitter) {
        ArrayList<String> result = new ArrayList<String>();
        for (String s : toSplit) {
            result.addAll(splitOn(s, splitter));
        }
        return result;
    }

    /**
     * This method concats elements if they contain open brackets so it's not split over multiple elements
     * @param result
     * @return
     */
    public static List<String> concatElementsWithBrackets(List<String> input) {
        List<String> result = new ArrayList<String>();
        int openBracketCounter = 0;
        String currentElement = null;
        for (String el : input) {
            openBracketCounter = openBracketCounter + getAmountOfCharInString('(', el)
                    - getAmountOfCharInString(')', el);

            if (currentElement == null) {
                currentElement = el;
            } else {
                currentElement += ", " + el;
            }
            if (openBracketCounter == 0) {
                result.add(currentElement);
                currentElement = null;
            }
        }

        return result;
    }

    /**********************************************
     *  Element counters
     ***********************************************/

    public static int getAmountOfCharInString(char toCount, String string) {
        int result = 0;
        for (int i = 0; i < string.length(); i++) {
            if (string.charAt(i) == toCount) {
                result += 1;
            }
        }
        return result;
    }
}

Related

  1. splitMultiMessageString( String multiMessageString, String messageSplitter)
  2. splitName(String name)
  3. splitNames(final String string)
  4. splitObjectString(String str)
  5. splitOgnl(String ognl)
  6. splitOnNoWiki(String s)
  7. splitOnTokens(String text)
  8. splitOnTokens(String text)
  9. splitOrderedDurationsIntoIntervals(String[] durations, int numberOfIntervals)