Here you can find the source of concatElementsWithBrackets(List
Parameter | Description |
---|---|
result | a parameter |
public static List<String> concatElementsWithBrackets(List<String> input)
//package com.java2s; //License from project: Apache License import java.util.ArrayList; import java.util.List; public class Main { /**// w w w. j a v a 2 s.com * This method concats elements if they contain open brackets so it's not split over multiple elements * @param result * @return */ public static List<String> concatElementsWithBrackets(List<String> input) { List<String> result = new ArrayList<String>(); int openBracketCounter = 0; String currentElement = null; for (String el : input) { openBracketCounter = openBracketCounter + getAmountOfCharInString('(', el) - getAmountOfCharInString(')', el); if (currentElement == null) { currentElement = el; } else { currentElement += ", " + el; } if (openBracketCounter == 0) { result.add(currentElement); currentElement = null; } } return result; } /********************************************** * Element counters ***********************************************/ public static int getAmountOfCharInString(char toCount, String string) { int result = 0; for (int i = 0; i < string.length(); i++) { if (string.charAt(i) == toCount) { result += 1; } } return result; } }