count sub string in a string - Android java.lang

Android examples for java.lang:String Substring

Description

count sub string 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.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  www  .  j  a  v  a2  s . c om*/
                index += substring.length();
            }
        }
        return count;
    }

}

Related Tutorials