黑马程序员技术交流社区
标题:
练习题
[打印本页]
作者:
Zhouyongming
时间:
2016-3-22 00:09
标题:
练习题
package com.heima.test;
public class Test4 {
/**
* 统计大串中小串出现的次数
*/
public static void main(String[] args) {
String str = "woshiheimachengxuyuan,heimanenggeiwomenyigegaoxingongzuo";
int count = 0;
String s = "g";
int i = 0;
//str.indexOf(s) 小字符串在大字符串中第一次出现的索引,如果小字符串不在大字符串中,索引的返回值是-1
while ((i=str.indexOf(s))!= -1) {
count ++;
str = str.substring(i+s.length()); //找到一次后需要把前面已经找到的小字符串截取掉,然后再进行下一次的查找
}
System.out.println(count);
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2