Here you can find the source of countChars(String text, char ch, int from, int to)
Parameter | Description |
---|---|
text | Text to be parsed |
ch | Character to be counted |
from | Text offset |
to | Text end |
public static int countChars(String text, char ch, int from, int to)
//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; } }