黑马程序员技术交流社区

标题: 练习题 [打印本页]

作者: Zhouyongming    时间: 2016-3-22 00:09
标题: 练习题
  1. package com.heima.test;

  2. public class Test4 {

  3.         /**
  4.          *  统计大串中小串出现的次数
  5.          */
  6.         public static void main(String[] args) {
  7.                 String str = "woshiheimachengxuyuan,heimanenggeiwomenyigegaoxingongzuo";
  8.                 int count = 0;
  9.                 String s = "g";
  10.                 int i = 0;
  11.                 //str.indexOf(s)                小字符串在大字符串中第一次出现的索引,如果小字符串不在大字符串中,索引的返回值是-1
  12.                 while ((i=str.indexOf(s))!= -1) {
  13.                        
  14.                         count ++;
  15.                         str = str.substring(i+s.length());                //找到一次后需要把前面已经找到的小字符串截取掉,然后再进行下一次的查找
  16.                                        
  17.                 }
  18.                 System.out.println(count);
  19.         }

  20. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2