Java String Occurence Count countOccurences(String textToSearch, String pattern)

Here you can find the source of countOccurences(String textToSearch, String pattern)

Description

count Occurences

License

Open Source License

Declaration

public static int countOccurences(String textToSearch, String pattern) 

Method Source Code

//package com.java2s;
/*/*  w  w  w .  ja  va2 s . c o  m*/
 * MiscUtil.java
 * miscellaneous utilities
 * Copyright (C) 2006 by Institute for Systems Biology,
 * Seattle, Washington, USA.  All rights reserved.
 *
 * This source code is distributed under the GNU Lesser
 * General Public License, the text of which is available at:
 *   http://www.gnu.org/copyleft/lesser.html
 */

public class Main {
    public static int countOccurences(String textToSearch, String pattern) {
        int base = 0;
        int count = 0;
        boolean done = false;
        while (!done) {
            base = textToSearch.indexOf(pattern, base);
            if (base > 0) {
                count++;
                base += 3;
            } else
                done = true;
        }
        return count;
    }
}

Related

  1. countOccurences(String str, int ch)
  2. countOccurences(String str, String subStr)
  3. countOccurences(String text, char find)
  4. countOccurences(String text, String subtext)
  5. countOccurences(String text, String target)
  6. countOccurencesOfChar(String str, char charToCount)