欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

判断String中包含某个字符串的个数,

来源: javaer 分享于  点击 7899 次 点评:209

判断String中包含某个字符串的个数,


  1. public class StringTest {  
  2.     private static int counter = 0;  
  3.     public static void main(String[] args) {  
  4.          String str1 = "sd& ^^java***java(((java";  
  5.          String str2 = "java";  
  6.          int i = countStrNum(str1, str2);  
  7.          System.out.println("str1中包含 " + i+" 个str2");  
  8.     }  
  9.   
  10.     /** 
  11.      * 判断str1中包含str2的个数 
  12.       * @param str1 
  13.      * @param str2 
  14.      * @return counter 
  15.      */  
  16.     public static int countStrNum(String str1, String str2) {  
  17.         if (str1.indexOf(str2) == -1) {  
  18.             return 0;  
  19.         } else if (str1.indexOf(str2) != -1) {  
  20.             counter++;  
  21.             countStr(str1.substring(str1.indexOf(str2) +  
  22.                    str2.length()), str2);  
  23.             return counter;  
  24.         }  
  25.         return 0;  
  26.     }
  27. }

相关文章

    暂无相关文章
相关栏目:

用户点评