Java String Explode explode(String string, String separators)

Here you can find the source of explode(String string, String separators)

Description

explode

License

Open Source License

Declaration

public static List<String> explode(String string, String separators) 

Method Source Code


//package com.java2s;
/*/*from   w  w  w.j  a  va2 s  .  c  o m*/
 * Copyright (c) 2014, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 *    Yatta Solutions - [466264] Enhance UX in simple installer
 */

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

public class Main {
    public static List<String> explode(String string, String separators) {
        return explode(string, separators, '\\');
    }

    public static List<String> explode(String string, String separators, char escapeCharacter) {
        List<String> tokens = new ArrayList<String>();

        StringBuilder builder = new StringBuilder();
        boolean separator = false;
        boolean escape = false;

        for (int i = 0; i < string.length(); i++) {
            separator = false;
            char c = string.charAt(i);
            if (!escape && c == escapeCharacter) {
                escape = true;
            } else {
                if (!escape && separators.indexOf(c) != -1) {
                    tokens.add(builder.toString());
                    builder.setLength(0);
                    separator = true;
                } else {
                    builder.append(c);
                }

                escape = false;
            }
        }

        if (separator || builder.length() != 0) {
            tokens.add(builder.toString());
        }

        return tokens;
    }
}

Related

  1. explode(String input, final char delimiter, final char escape, final int capacity)
  2. explode(String s, String delim)
  3. explode(String source, String deliminator)
  4. explode(String split, String input)
  5. explode(String src, String sep)
  6. explode(String tokenizer, String text)
  7. explode(String[] elements, String separator)
  8. explodeCamelCase(String str, boolean excludeGetOrSet)
  9. explodeDomains(final String fqdn)