Java Char Count countChar(String a_text, char a_character)

Here you can find the source of countChar(String a_text, char a_character)

Description

Count the number of a single character that occur in a string.

License

Open Source License

Parameter

Parameter Description
a_text the string to search
a_character the character to count.

Return

the number of characters in the string.

Declaration

public static int countChar(String a_text, char a_character) 

Method Source Code

//package com.java2s;
/*//from w  w  w. j  a va 2s . c o  m
 * @(#) StringUtils.java Jul 20, 2005
 * Copyright 2005 Frequency Marketing, Inc. All rights reserved.
 * Frequency Marketing, Inc. PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {
    /**
     * Count the number of a single character that occur in a string.
     * @param a_text the string to search
     * @param a_character the character to count.
     * @return the number of characters in the string.
     */
    public static int countChar(String a_text, char a_character) {
        int count = 0;

        String text = a_text;

        int pos = text.indexOf(a_character);
        while (pos != -1) {
            count++;

            pos = text.indexOf(a_character, pos + 1);
        }

        return count;
    }
}

Related

  1. countChar(CharSequence chars, char c)
  2. countChar(final int[] locator, final char c)
  3. countChar(final String aString, final char aChar)
  4. countChar(final String host, final char charactor)
  5. countChar(int start_index, int end_index, StringBuffer txt, char c)
  6. countCharacter(final String sourceString, final char lookFor)
  7. countCharacter(String content, char character)
  8. countCharacterOccurrences(String string, char c)
  9. countCharacters(String code, char c, int start, int end)