Here you can find the source of countOccurencesOfChar(String str, char charToCount)
public static int countOccurencesOfChar(String str, char charToCount)
//package com.java2s; /******************************************************************************* * * Copyright (c) 2016 ecFeed AS. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * /*from w ww . j a va2s. c om*/ *******************************************************************************/ public class Main { public static int countOccurencesOfChar(String str, char charToCount) { int len = str.length(); int occurences = 0; String strgToCount = Character.toString(charToCount); for (int index = 0; index < len; ++index) { String substr = str.substring(index, index + 1); if (strgToCount.equals(substr)) { occurences++; } } return occurences; } }