Java String Char Count countChar(String str, char chr)

Here you can find the source of countChar(String str, char chr)

Description

Returns the number of occurrences of a specific character.

License

Open Source License

Parameter

Parameter Description
str is the string containing the character to look for.
chr is the character we look for.

Return

the number of occurrences.

Declaration

public static int countChar(String str, char chr) 

Method Source Code

//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;
    }
}

Related

  1. countChar(String s, char c)
  2. countChar(String sql, int end)
  3. countChar(String src, char c)
  4. countChar(String str, char c)
  5. countChar(String str, char c)
  6. countChar(String str, char reg)
  7. countChar(String string, char c, int pos, boolean forwards)
  8. countChar(String string, String c)
  9. countChar(String text, char c)