Java String Occurence Count countOccurencesOfChar(String str, char charToCount)

Here you can find the source of countOccurencesOfChar(String str, char charToCount)

Description

count Occurences Of Char

License

Open Source License

Declaration

public static int countOccurencesOfChar(String str, char charToCount) 

Method Source Code

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

Related

  1. countOccurences(String str, String subStr)
  2. countOccurences(String text, char find)
  3. countOccurences(String text, String subtext)
  4. countOccurences(String text, String target)
  5. countOccurences(String textToSearch, String pattern)