Java Char Count charCount(String str, char c, boolean countEscaped)

Here you can find the source of charCount(String str, char c, boolean countEscaped)

Description

Helper method to count number of given characters in a string.

License

Mozilla Public License

Parameter

Parameter Description
str to check characters for.
c character to count
countEscaped <tt>true</tt> will included escaped characters as part of the count`

Return

number of occurrences of the character C in the given string.

Declaration

public static int charCount(String str, char c, boolean countEscaped) 

Method Source Code

//package com.java2s;
/*// ww w .  j a  v a  2  s . co m
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */

public class Main {
    /**
     * Helper method to count number of given characters in a string.
     * <p>
     * This will return number of character instances in the given string. if the given string is null then a -1 will
     * returned.
     * <p>
     * For example if the given string is "XXNNXXnxXnXN" and the char is 'n' it will return a value of two as this
     * method is case sensitive.
     * <p>
     * Another example is if given the string 'X\nXxxNnN' with count escaped set to false will return 1 otherwise it
     * will return 2.
     * 
     * @param str to check characters for.
     * @param c character to count
     * @param countEscaped <tt>true</tt> will included escaped characters as part of the count`
     * @return number of occurrences of the character C in the given string.
     */
    public static int charCount(String str, char c, boolean countEscaped) {

        if (str == null)
            return -1;

        int cnt = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == c) {
                if (i >= 1) {
                    if (str.charAt(i - 1) == '\\') {
                        if (countEscaped) {
                            cnt++;
                        } else {
                            continue;
                        }
                    } else {
                        cnt++;
                    }
                }
            }
        }
        return cnt;
    }
}

Related

  1. charCount(final String s, final char c)
  2. charCount(final String s, final char c)
  3. charCount(int codePoint)
  4. charCount(String s, char c)
  5. charCount(String source, char target)
  6. charCount(String str, char chr)
  7. charCount(String str, char sub)
  8. charCount(String subject, String needle)
  9. charCountRight(String str, String sub)