- package com.it_string;
- public class StringDemo4 {
- public static void main(String[] args) {
- /*
- * BBBBAABBBBAABBBBB
- 统计AA出现的次数
- */
- String maxs="BBAABBBBAABBBB";
- String mins="AA";
- int count = getCount(maxs, mins);
- System.out.println("count:"+count);
- }
- public static int getCount(String maxs, String mins) {
- int count =0;
- int index = maxs.indexOf(mins);
- while(index!=-1){
- count++;
- int starti=index+mins.length();
- maxs=maxs.substring(starti);
- index=maxs.indexOf(mins);
- }
- return count;
- }
- }
复制代码 |
|