Java String Char Count countChar(String string, char c, int pos, boolean forwards)

Here you can find the source of countChar(String string, char c, int pos, boolean forwards)

Description

Count how many times a character is repeated in a string, starting at a given position and moving forwards or backwards.

License

Open Source License

Declaration

public static int countChar(String string, char c, int pos, boolean forwards) 

Method Source Code

//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;
    }
}

Related

  1. countChar(String src, char c)
  2. countChar(String str, char c)
  3. countChar(String str, char c)
  4. countChar(String str, char chr)
  5. countChar(String str, char reg)
  6. countChar(String string, String c)
  7. countChar(String text, char c)
  8. countChar(String text, char ch)
  9. countChar(StringBuffer sb, char ch)