find the count of findStr in srcStr - Android java.lang

Android examples for java.lang:String Algorithm

Description

find the count of findStr in srcStr

Demo Code

import android.graphics.Paint;
import android.widget.TextView;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**/*  ww  w .ja  v  a2 s.  c  om*/
     * find the count of findStr in srcStr
     * 
     * @param srcStr
     * @param findStr
     * @return
     */
    public static int countStringNumbers(String srcStr, String findStr) {
        int counter = 0;
        if (srcStr.indexOf(findStr) == -1) {
            counter = 0;
        } else if (srcStr.indexOf(findStr) != -1) {
            counter++;
            counter = counter
                    + countStringNumbers(
                            srcStr.substring(srcStr.indexOf(findStr)
                                    + findStr.length()), findStr);
        }
        return counter;
    }

}

Related Tutorials