Java String Split by Delimiter stringSplitter(String inStr, String delimiterStr)

Here you can find the source of stringSplitter(String inStr, String delimiterStr)

Description

string Splitter

License

Open Source License

Declaration

public static String[] stringSplitter(String inStr, String delimiterStr) 

Method Source Code


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

import java.util.ArrayList;

public class Main {
    public static String[] stringSplitter(String inStr, String delimiterStr) {
        ArrayList<String> stringVector = new ArrayList<String>();
        String beforeStr;//from   w  w w.j  av  a2 s .c o  m
        while ((beforeStr = stringBefore(inStr, delimiterStr)) != null) {
            stringVector.add(beforeStr);
            inStr = stringAfter(inStr, delimiterStr);
        }
        stringVector.add(inStr);

        return stringVector.toArray(new String[] {});
    }

    public static String stringBefore(String inStr, String delimiterStr) {
        int pos = inStr.indexOf(delimiterStr);
        if (pos == -1)
            return null;
        else
            return inStr.substring(0, pos);
    }

    /**
     * Returns the string before a delimiter string.
     *
     * @param inStr input string
     * @param delimiterStr delimiter
     * @param nr 'f' for first, 'l' for last
     * @return string before a delimiter string
     */
    public static String stringBefore(String inStr, String delimiterStr, char nr) {
        if (nr == 'f') {
            return stringBefore(inStr, delimiterStr);
        } else {
            int pos = inStr.lastIndexOf(delimiterStr);
            if (pos == -1)
                return null;
            else
                return inStr.substring(0, pos);
        }
    }

    public static String stringAfter(String inStr, String delimiterStr) {
        int pos = inStr.indexOf(delimiterStr);
        if (pos == -1)
            return null;
        else
            return inStr.substring(pos + delimiterStr.length(), inStr.length());
    }

    /**
     * Returns the string after a delimiter string.
     *
     * @param inStr input string
     * @param delimiterStr delimiter
     * @param nr 'f' for first, 'l' for last
     * @return string after a delimiter string
     */
    public static String stringAfter(String inStr, String delimiterStr, char nr) {
        if (nr == 'f') {
            return stringAfter(inStr, delimiterStr);
        } else {
            int pos = inStr.lastIndexOf(delimiterStr);
            if (pos == -1)
                return null;
            else
                return inStr.substring(pos + delimiterStr.length(), inStr.length());
        }
    }
}

Related

  1. splitToList(String s, String delimRegEx)
  2. splitToList(String str, char delimiter)
  3. splitUpTo(String s, String delimiter, int cnt)
  4. splitWhileKeepingParentheses(String value, char delimiter)
  5. splitWithEscapeChar(String s, char delimiter, char escape)
  6. tokenizerSplit(String string, String delimiter)
  7. toTextArray(String text, String delimiter)