Here you can find the source of countCharsInString(final String stringToCountOccurrancesIn, final char characterToCount)
Parameter | Description |
---|---|
stringToCountOccurrancesIn | <code>String</code> to count occurrances in |
characterToCount | <code>char</code> whose occurrances will be counted |
int
with the count of the given character
public static int countCharsInString(final String stringToCountOccurrancesIn, final char characterToCount)
//package com.java2s; //License from project: BSD License public class Main { /**/* ww w . ja v a 2s . co m*/ * Count occurrances of a character in a string * * @param stringToCountOccurrancesIn * <code>String</code> to count occurrances in * @param characterToCount * <code>char</code> whose occurrances will be counted * @return <code>int</code> with the count of the given character */ public static int countCharsInString(final String stringToCountOccurrancesIn, final char characterToCount) { if (stringToCountOccurrancesIn == null) { throw new IllegalArgumentException("null s"); } final char[] chars = stringToCountOccurrancesIn.toCharArray(); int count = 0; for (char c : chars) { if (c == characterToCount) { count++; } } return count; } }