Java Char Count countChar(final String aString, final char aChar)

Here you can find the source of countChar(final String aString, final char aChar)

Description

count Char

License

Open Source License

Parameter

Parameter Description
aString a parameter
aChar a parameter

Return

the number of the searched char int the string

Declaration

public static int countChar(final String aString, final char aChar) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 www.isandlatech.com (www.isandlatech.com)
 * 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
 *
 * Contributors://from  w w  w .j a  va  2  s.  co m
 *    ogattaz (isandlaTech) - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * 
     * @param aString
     * @param aChar
     * @return the number of the searched char int the string
     */
    public static int countChar(final String aString, final char aChar) {

        if (aString == null) {
            return -1;
        }

        int wCount = 0;
        int wMax = aString.length();
        int wI = 0;
        while (wI < wMax) {
            if (aString.charAt(wI) == aChar) {
                wCount++;
            }
            wI++;
        }
        return wCount;
    }
}

Related

  1. charCountRight(String str, String sub)
  2. countChar(char charIn, String stingIn)
  3. countChar(char[] line, char target_char)
  4. countChar(CharSequence chars, char c)
  5. countChar(final int[] locator, final char c)
  6. countChar(final String host, final char charactor)
  7. countChar(int start_index, int end_index, StringBuffer txt, char c)
  8. countChar(String a_text, char a_character)
  9. countCharacter(final String sourceString, final char lookFor)