Java String Char Count countChars(String s, char c)

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

Description

count Chars

License

Apache License

Declaration

public static int countChars(String s, char c) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static int countChars(String s, char c) {
        int cnt = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                cnt++;/*from w w w  . j av  a 2s .co m*/
            }
        }
        return cnt;
    }

    public static int countChars(String s, char... chars) {
        int cnt = 0;
        for (int i = 0; i < s.length(); i++) {
            char cs = s.charAt(i);
            for (char c : chars) {
                if (cs == c) {
                    cnt++;
                    break;
                }
            }
        }
        return cnt;
    }
}

Related

  1. countChar(StringBuffer sb, char ch)
  2. countChars(String data)
  3. countChars(String input)
  4. countChars(String s, boolean countDigits, boolean countLetters, boolean countOthers)
  5. countChars(String s, char c)
  6. countChars(String s, char c)
  7. countChars(String s, String charset)
  8. countChars(String string, char c)
  9. countChars(String string, int character)