int (@NotNull String text, final int from, final int toExcluding, final String s) { int res = 0; int i = from; final int limit = min(text.length(), toExcluding); while (i < limit) { i = text.indexOf(s, i); if (i >= 0 && i < limit) { res++; i+=s.length(); } else { break; } } return res; }