Count Occurances Of String - Java java.lang

Java examples for java.lang:String Algorithm

Description

Count Occurances Of String

Demo Code


//package com.java2s;

public class Main {
    public static int CountOccurancesOf(String s, String line) {
        return (line.length() - line.replace(s, "").length());
    }/* ww  w  .  j  a v a2 s  . c om*/

    public static int CountOccurancesOf(char s, StringBuilder builder) {
        int count = 0;
        for (int i = 0; i < builder.length(); i++) {
            char c = builder.charAt(i);
            if (c == s) {
                count++;
            }
        }
        return count;
    }
}

Related Tutorials