gf-jie
 

方法一:不断截取源字符串

 1 String  str1  =  "nihaokogaokoshaokoxia "; 
 2 String  str2  =  "oko "; 
 3 int  total  =  0; 
 4 for  (String  tmp  =  str1;  tmp  !=  null&&tmp.length()> =str2.length();){ 
 5   if(tmp.indexOf(str2)  ==  0){ 
 6     total  ++; 
 7     tmp  =  tmp.substring(str2.length()); 
 8   }else{ 
 9     tmp  =  tmp.substring(1); 
10   } 
11 } 
12 System.out.println(str1+ "中含有 "+total+ "个 "+str2);

方式二,字符串转数组

1 String  str1  =  "nihaoksdoksad "; 
2 char  []c=str1.toCharArray(); 
3 int  total=0; 
4 for(int  i=0;i <c.length-1;i++) 
5 if(c[i]== \'o \'&&c[i+1]== \'k \') 
6 total++; 
7 System.out.println(str1+ "中含有 "+total+ "个ok ");
8  

 

分类:

技术点:

相关文章:

  • 2021-05-20
  • 2021-07-25
  • 2021-12-28
  • 2021-11-21
  • 2021-07-19
  • 2021-09-20
  • 2022-01-08
  • 2022-01-08
猜你喜欢
  • 2022-01-07
  • 2022-01-07
  • 2022-01-17
  • 2021-09-17
  • 2022-02-28
  • 2022-01-08
相关资源
相似解决方案