Here you can find the source of countChar(String str, char chr)
Parameter | Description |
---|---|
str | is the string containing the character to look for. |
chr | is the character we look for. |
public static int countChar(String str, char chr)
//package com.java2s; /**/* w w w . j a v a2 s . c o m*/ * Copyright (c) 2001-2016 Mathew A. Nelson and Robocode contributors * 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://robocode.sourceforge.net/license/epl-v10.html */ public class Main { /** * Returns the number of occurrences of a specific character. * * @param str is the string containing the character to look for. * @param chr is the character we look for. * @return the number of occurrences. */ public static int countChar(String str, char chr) { int count = 0; for (char c : str.toCharArray()) { if (c == chr) { count++; } } return count; } }