Java Char Count countCharInString(String s, char c)

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

Description

count Char In String

License

Open Source License

Declaration

public static int countCharInString(String s, char c) 

Method Source Code

//package com.java2s;
/**//from   ww w.j a v  a 2s .  c  om
 * Title:        efa - elektronisches Fahrtenbuch f?r Ruderer
 * Copyright:    Copyright (c) 2001-2011 by Nicolas Michael
 * Website:      http://efa.nmichael.de/
 * License:      GNU General Public License v2
 *
 * @author Nicolas Michael
 * @version 2
 */

public class Main {
    public static int countCharInString(String s, char c) {
        int n = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                n++;
            }
        }
        return n;
    }

    public static int countCharInString(String s, String c) {
        int n = 0;
        int pos;
        while ((pos = s.indexOf(c)) >= 0) {
            n++;
            s = s.substring(pos + c.length());
        }
        return n;
    }
}

Related

  1. countCharacter(final String sourceString, final char lookFor)
  2. countCharacter(String content, char character)
  3. countCharacterOccurrences(String string, char c)
  4. countCharacters(String code, char c, int start, int end)
  5. countCharacters(String str, char chr)
  6. countChars( final String testString, final char match )
  7. countChars(final String str, final char c)
  8. countChars(final String str, final char chr)
  9. countChars(final String string, final String findStr)