Java String Split by Comma splitComma(final String strWithComma)

Here you can find the source of splitComma(final String strWithComma)

Description

split Comma

License

Apache License

Declaration

private static List<String> splitComma(final String strWithComma) 

Method Source Code

//package com.java2s;
/**/*from w w  w.  ja va  2s.  c  o  m*/
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    private static List<String> splitComma(final String strWithComma) {
        List<String> result = new ArrayList<>();
        StringBuilder token = new StringBuilder();
        int closureDepth = 0;
        boolean isStartMatch = false;
        for (int i = 0; i < strWithComma.length(); i++) {
            char c = strWithComma.charAt(i);
            switch (c) {
            case '$':
                isStartMatch = true;
                token.append(c);
                break;
            case '{':
                closureDepth = isStartMatch ? closureDepth + 1 : closureDepth;
                isStartMatch = false;
                token.append(c);
                break;
            case '}':
                closureDepth--;
                isStartMatch = false;
                token.append(c);
                break;
            case ',':
                if (closureDepth > 0) {
                    token.append(c);
                } else {
                    result.add(token.toString());
                    token.setLength(0);
                }
                break;
            default:
                token.append(c);
                break;
            }
        }
        if (token.length() > 0) {
            result.add(token.toString());
        }
        return result;
    }
}

Related

  1. splitAtCommaOrPipe(String input)
  2. splitComma(String configValue)
  3. splitCommand(String command)
  4. splitCommand(String command)
  5. splitCommand(String command)