Here you can find the source of split(String text)
private static List<String> split(String text)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { private static List<String> split(String text) { final ArrayList<String> result = new ArrayList<>(); String newText = ""; Scanner scan = new Scanner(text).useDelimiter(" "); while (scan.hasNext()) { newText += scan.next();/* ww w . java 2 s .com*/ final int openingIdx = newText.indexOf("("); if (openingIdx == -1) { result.add(newText); newText = ""; } else { final int closingIdx = indexOfClosingBracket(newText, openingIdx); if (closingIdx == -1) { newText += " "; } else { result.add(newText); newText = ""; } } } return result; } private static int indexOfClosingBracket(String text, int openingBracket) { int result = -1; ArrayDeque<String> stack = new ArrayDeque<>(); for (int i = openingBracket; i < text.length(); i++) { if (text.charAt(i) == '(') { stack.push("("); } else if (text.charAt(i) == ')') { if (stack.isEmpty()) { break; } stack.pop(); if (stack.isEmpty()) { result = i; break; } } } return result; } }