Here you can find the source of countChar(String string, char c, int pos, boolean forwards)
public static int countChar(String string, char c, int pos, boolean forwards)
//package com.java2s; //it under the terms of the GNU Affero General Public License as published by public class Main { /**//from ww w. j a v a2 s. com * Count how many times a character is repeated in a string, * starting at a given position and moving forwards or backwards. */ public static int countChar(String string, char c, int pos, boolean forwards) { int count = 0; int increment = 1; if (!forwards) increment = -1; for (int i = pos; i >= 0 && i < string.length(); i += increment) { char c2 = string.charAt(i); if (c2 == c) count++; else break; } return count; } }