Java String Char Count countChars(String text, char ch, int from, int to)

Here you can find the source of countChars(String text, char ch, int from, int to)

Description

Counts number of specified characters in give text.

License

Open Source License

Parameter

Parameter Description
text Text to be parsed
ch Character to be counted
from Text offset
to Text end

Return

Number of character occurences in given text.

Declaration

public static int countChars(String text, char ch, int from, int to) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w  w  w.  j  ava  2 s.com*/
     * Counts number of specified characters in give text.
     * @param text Text to be parsed
     * @param ch Character to be counted
     * @param from Text offset
     * @param to Text end
     * @return Number of character occurences in given text.
     */
    public static int countChars(String text, char ch, int from, int to) {
        int textLen = text.length();
        if (from < 0) {
            from = 0;
        }
        if (to >= textLen) {
            to = textLen - 1;
        }

        int count = 0;
        for (int i = from; i <= to; i++) {
            if (text.charAt(i) == ch) {
                count++;
            }
        }

        return count;
    }
}

Related

  1. countChars(String s, char c)
  2. countChars(String s, char c)
  3. countChars(String s, String charset)
  4. countChars(String string, char c)
  5. countChars(String string, int character)
  6. countCharsInString(final String stringToCountOccurrancesIn, final char characterToCount)