Java String Split by Separator splitStructs(String str, char separator)

Here you can find the source of splitStructs(String str, char separator)

Description

split Structs

License

Open Source License

Declaration

private static String[] splitStructs(String str, char separator) 

Method Source Code

//package com.java2s;
/**/*from   www.j a  va2s.  com*/
 * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
 * except in compliance with the License. A copy of the License is located at
 *
 * ??? http://aws.amazon.com/apache2.0/
 *
 * or in the "LICENSE.TXT" file accompanying this file. This file 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.
 */

import java.util.ArrayList;

public class Main {
    private static String[] splitStructs(String str, char separator) {
        if (str == null) {
            return null;
        }
        int len = str.length();
        if (len == 0) {
            return null;
        }
        ArrayList<String> list = new ArrayList<String>();
        int index = 0;
        int start = 0;
        int match = 0;
        while (index < len) {
            if (str.charAt(index) == '<') {
                match++;
            } else if (str.charAt(index) == '>') {
                match--;
            } else if (str.charAt(index) == separator) {
                if (match == 0) {
                    list.add(str.substring(start, index).trim());
                    start = ++index;
                    continue;
                }
            }
            index++;
        }

        if (match != 0) {
            return null;
        }
        list.add(str.substring(start, index).trim());
        String[] result = new String[list.size()];
        list.toArray(result);

        return result;
    }
}

Related

  1. splitSmart(String s, char separator)
  2. splitStaySeparator(String str, char token)
  3. splitString(String sInput, String sSeparator)
  4. splitStringToLong(String strInput, String separator)
  5. splitStringWithBracesOnSeparator(String string, char separator)
  6. splitToList(String s, String separator)
  7. splitValues(String concatedValues, String separator)
  8. splitValues(String values, String separator)
  9. splitWithBreakingBrace(String str, char separator, char braceStart, char braceEnd)