方法一

public int count(String srcText, String findText) {  
        String[] array = srcText.split(findText);  
        return array.length - 1;  
}  

方法二

public static int count(String srcText, String findText) {  
    int count = 0;  
    Pattern p = Pattern.compile(findText);  
    Matcher m = p.matcher(srcText);  
    while (m.find()) {  
        count++;  
    }  
    return count;  
}  

方法三

public static int count(String srcText, String findText) {  
    int count = 0;  
    int index = 0;  
    while ((index = srcText.indexOf(findText, index)) != -1) {  
        index = index + findText.length();  
        count++;  
    }  
    return count;  
}

 

相关文章:

  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2022-02-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-28
  • 2021-12-28
  • 2021-09-02
  • 2021-07-21
相关资源
相似解决方案