Java String Split by Char splitChars(String input, String charsToBeRemoved)

Here you can find the source of splitChars(String input, String charsToBeRemoved)

Description

Split the input string considering as string separator the chars specified in the input variable charsToBeRemoved.

License

Open Source License

Parameter

Parameter Description
input The string in input.
charsToBeRemoved The chars to be removed.

Return

The array of strings as output.

Declaration

public static String[] splitChars(String input, String charsToBeRemoved) 

Method Source Code

//package com.java2s;
// modify it under the terms of the Common Public License; either

import java.util.ArrayList;

public class Main {
    /**/*from ww w  .ja  v a  2  s .  com*/
     * Split the input string considering as string separator
     * the chars specified in the input variable charsToBeRemoved.
     * <BR>For example if you call splitChars(&quot;&lt;DIV&gt;  +12.5, +3.4 &lt;/DIV&gt;&quot;, &quot; <>DIV/,&quot;),
     * <BR>you obtain an array of strings {&quot;+12.5&quot;, &quot;+3.4&quot;} as output (space chars and &lt;,&gt;,D,I,V,/ and the comma are chars that must be removed).
     * @param input The string in input.
     * @param charsToBeRemoved The chars to be removed.
     * @return The array of strings as output.
    */
    public static String[] splitChars(String input, String charsToBeRemoved) {

        ArrayList output = new ArrayList();
        int minCapacity = 0;
        StringBuffer str = new StringBuffer();

        boolean charFound = false;
        boolean toBeAdd = false;
        for (int index = 0; index < input.length(); index++) {
            charFound = false;
            for (int charsCount = 0; charsCount < charsToBeRemoved.length(); charsCount++)
                if (charsToBeRemoved.charAt(charsCount) == input.charAt(index))
                    charFound = true;
            if (!(charFound)) {
                str.append(input.charAt(index));
                toBeAdd = false;
            } else if (!toBeAdd)
                toBeAdd = true;
            // finished to parse one string
            if (toBeAdd && (str.length() != 0)) {
                minCapacity++;
                output.ensureCapacity(minCapacity);
                if (output.add(str.toString()))
                    str = new StringBuffer();
                else
                    minCapacity--;
            }
        }
        // add the last string
        if (str.length() != 0) {
            minCapacity++;
            output.ensureCapacity(minCapacity);
            if (output.add(str.toString()))
                str = new StringBuffer();
            else
                minCapacity--;
        }

        output.trimToSize();
        Object[] outputObj = output.toArray();
        String[] outputStr = new String[output.size()];
        for (int i = 0; i < output.size(); i++)
            outputStr[i] = new String((String) outputObj[i]);
        return outputStr;

    }
}

Related

  1. split(String value, char splitChar)
  2. splitAt(String inputString, Character inputChar)
  3. splitAt(String str, char c)
  4. splitByChar(final String message, final char ch)
  5. splitByNonNestedChars(String s, char... c)
  6. splitEncolosed(String s, char open_tag, char close_tag)
  7. splitFast3(String data, char splitChar)
  8. splitInclusive(CharSequence input, char[] splitChars)
  9. splitList(String source, char useChar)