Here you can find the source of nestedIndexOf(final String s, final int startPos, final String open, final String close)
public static int nestedIndexOf(final String s, final int startPos, final String open, final String close)
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); public class Main { public static int nestedIndexOf(final String s, final int startPos, final String open, final String close) { int depth = 0; for (int i = startPos; i < s.length();) { if (s.startsWith(close, i)) { if (depth == 0) { return i; } else { --depth;//from w ww .j ava 2 s .c o m i += close.length(); } } else if (s.startsWith(open, i)) { ++depth; i += open.length(); } else { ++i; } } return -1; } }