编写一个方法,输出在一个字符串中,指定字符串出现的次数。


 1 public class Test {
 2     public static void main(String args[]) {
 3         String s = "I love java and I want to learn java!";
 4         String findString = "java";
 5         int count = Test.countString(s,findString);
 6         System.out.print("字符串个数为: " + count);
 7     }
 8     
 9     public static int countString(String s1, String s2) {
10         int count = 0; 
11         int findIndex = 0;
12         int findLen = s2.length();
13         findIndex = s1.indexOf(s2);
14         while(findIndex != -1){
15             s1 = s1.substring(findIndex + findLen);
16             findIndex = s1.indexOf(s2);
17             count++;
18         }
19         return count;
20     }
21     
22 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM