Java Char Count charCount(final String s, final char c)

Here you can find the source of charCount(final String s, final char c)

Description

Get the number of the occurence of a char in a string.

License

GNU General Public License

Parameter

Parameter Description
s String to parse
c Char to find

Return

the number of the char occurence

Declaration

public static final int charCount(final String s, final char c) 

Method Source Code

//package com.java2s;
/*//from www  .ja v a 2  s . co  m
 *                  Corsen development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU General Public Licence version 2 or later. This
 * should be distributed with the code. If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/licenses/gpl-2.0.txt
 *
 * Copyright for this code is held jointly by the microarray platform
 * of the ?cole Normale Sup?rieure and the individual authors.
 * These should be listed in @author doc comments.
 *
 * For more information on the Corsen project and its aims,
 * or to join the Corsen google group, visit the home page
 * at:
 *
 *      http://transcriptome.ens.fr/corsen
 *
 */

public class Main {
    /**
     * Get the number of the occurence of a char in a string.
     * @param s String to parse
     * @param c Char to find
     * @return the number of the char occurence
     */
    public static final int charCount(final String s, final char c) {

        if (s == null)
            return 0;

        final int len = s.length();
        int count = 0;

        for (int i = 0; i < len; i++)
            if (s.charAt(i) == c)
                count++;

        return count;
    }
}

Related

  1. charCount(final String s, final char c)
  2. charCount(int codePoint)
  3. charCount(String s, char c)
  4. charCount(String source, char target)