A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Zhouyongming 中级黑马   /  2016-3-22 00:09  /  311 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  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. }
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马