count substring in a string - Android java.lang

Android examples for java.lang:String Substring

Description

count substring in a string

Demo Code

import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Main{

    public static int count(String str, String substring) {
        int index = 0;
        int count = 0;
        while (index != -1) {
            index = str.indexOf(substring, index);
            if (index != -1) {
                count++;/*from   w  ww.  j a v a2s .c  o  m*/
                index += substring.length();
            }
        }
        return count;
    }

}

Related Tutorials