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

  1. /*
  2. 3.获取一个字符串在另一个字符串中出现的次数.
  3.         "abkkcdkkefkkskkgkk";
  4.         思路:
  5.                 通过循环 indexOf 方法,创建计数器count和标记index,
  6.                 字符串每出现一次:i自增,k = 所在位置 + 1;
  7. */
  8. public class StringTest {

  9.         public static void main(String[] args) {
  10.        
  11.                 String str = "abkkcdkkefkkskkgkk";
  12.                 String key = "kkk";
  13.                
  14.                 int count = getCount_3(str,key);
  15.                 System.out.println(key + "在" + str + "中出现" + count + "次.");
  16.         }
  17.         /*
  18.         方法总结: 总体思路与视频教程相仿,但细节却完全不是一个路线,而且我这个无论
  19.                                 用whlie还是for循环都会出现无限循环,使用do whlie 也是无奈之举.
  20.         */
  21.         public static int getCount(String str, String key) {
  22.                 int count=0,k = key.length(),index = -k;
  23.                 do {
  24.                         index = str.indexOf(key,index+k);
  25.                         System.out.println("index= " + index);
  26.                         count++;
  27.                 }
  28.                 while(index != -1);
  29.                
  30.                 return count-1;
  31.         }
  32.        
  33.        
  34.         /*
  35.         毕老师视频方法:
  36.         */
  37.         public static int getCount_1(String str, String key) {
  38.        
  39.                 int count = 0, index = 0;
  40.                
  41.                 while((index = str.indexOf(key)) != -1) {
  42.                
  43.                         str = str.substring(index+key.length());
  44.                        
  45.                         count++;
  46.                 }
  47.                 return count;
  48.         }
  49.        
  50.         /*
  51.         看完毕老师视频优化自己方法:
  52.         */
  53.         public static int getCount_2(String str, String key) {
  54.                 int count=0, index = 0;
  55.                 while(index != -1 && (index = str.indexOf(key,index)) != -1) {
  56.                
  57.                         //System.out.println("index= " + index);
  58.                         index = str.indexOf(key, index + key.length());
  59.                         count++;
  60.                 }
  61.                 return count;
  62.         }
  63.        
  64.         /*
  65.         总结,其实优化后也没有多简洁,但好在把老师的思路加入到了自己的代码当中. 继续努力!
  66.         */
  67.        
  68.         /*
  69.         好吧,继续看完后半部分视频之后,给自己跪了...饶了个大圈子把自己绕晕...
  70.         */
  71.        
  72.         public static int getCount_3(String str,String key) {
  73.                 int count=0, index = 0;
  74.                 while((index = str.indexOf(key, index)) != -1) {
  75.                
  76.                         System.out.println("index= " + index);
  77.                         index += key.length();
  78.                         count++;
  79.                 }
  80.                 return count;
  81.         }
  82. }
复制代码
您需要登录后才可以回帖 登录 | 加入黑马