int (@NotNull CharSequence text, char c, int start, int end, boolean stopAtOtherChar) { boolean forward = start <= end; start = forward ? Math.max(0, start) : Math.min(text.length(), start); end = forward ? Math.min(text.length(), end) : Math.max(0, end); int count = 0; for (int i = forward ? start : start - 1; forward == i < end; i += forward ? 1 : -1) { if (text.charAt(i) == c) { count++; } else if (stopAtOtherChar) { break; } } return count; }