Java String Split by Comma splitAtCommaOrPipe(String input)

Here you can find the source of splitAtCommaOrPipe(String input)

Description

split At Comma Or Pipe

License

Apache License

Declaration

public static String[] splitAtCommaOrPipe(String input) 

Method Source Code

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

import java.util.ArrayList;

public class Main {
    public static String[] splitAtCommaOrPipe(String input) {
        ArrayList<String> result = new ArrayList<String>();

        char character = '\0';
        char previousCharacter;

        StringBuilder value = new StringBuilder();
        for (int i = 0; i < input.length(); i++) {
            previousCharacter = character;
            character = input.charAt(i);

            if (character == ',' || character == '|') {
                if (previousCharacter == '\\') {
                    value.setCharAt(value.length() - 1, character);
                    continue;
                }//from w ww.  j  av a2  s .  c  om
                result.add(value.toString().trim());
                value = new StringBuilder();
                continue;
            }

            value.append(character);
        }
        result.add(value.toString().trim());

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

Related

  1. splitComma(final String strWithComma)
  2. splitComma(String configValue)
  3. splitCommand(String command)
  4. splitCommand(String command)