黑马程序员技术交流社区
标题:
获取"喇嘛与哑巴"这个段子中喇叭出现的次数~~~~
[打印本页]
作者:
xiejun0725
时间:
2015-5-5 23:39
标题:
获取"喇嘛与哑巴"这个段子中喇叭出现的次数~~~~
public class StringTest {
public static void main(String[] args) {
String str = "打南边来了个哑巴,腰里别了个喇叭,打北边来了个喇嘛,手里提了个獭犸,/n/r" +
"提着獭犸的喇嘛要拿獭犸换别着喇叭的哑巴的喇叭,别着喇叭的哑巴不愿拿喇叭换提着獭犸的喇嘛的獭犸,/n/r" +
"不知是别着喇叭的哑巴打了提着獭犸的喇嘛一喇叭,还是提着獭犸的喇嘛打了别着喇叭的哑巴一獭犸,/n/r" +
"喇嘛回家炖獭犸,哑巴嘀嘀哒哒吹喇叭";
String regex = "喇嘛";
// 功能
int count = getCount(str, regex);
System.out.println(count);
}
public static int getCount(String maxString, String minString) {
// 定义统计变量
int count = 0;
// 在大串中查找小串一次
int index = 0;
// 如果返回值不是-1,说明小串在大串中是存在的。
// 判断
while ((index = maxString.indexOf(minString)) != -1) {
// 统计变量++
count++;
// 把查找过的数据给截取掉,重新赋值给大串
maxString = maxString.substring(index + minString.length());
}
return count;
}
}
作者:
殃金咒
时间:
2015-5-6 00:23
我靠,高人啊= =
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2