Here you can find the source of indexOfClosingBracket(String text, int openingBracket)
private static int indexOfClosingBracket(String text, int openingBracket)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { 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; }/*w ww.j a va2s .c o m*/ stack.pop(); if (stack.isEmpty()) { result = i; break; } } } return result; } }