首页 > 开发 > Java > 正文

Java获取指定字符串出现次数的方法

2024-07-13 10:14:26
字体:
来源:转载
供稿:网友

Java中 获取指定字符串在另一个字符串中出现的次数,供大家参考,具体内容如下

/**  * @param args  */ public static void main(String[] args) {    String srcText = "Hello World";   String findText = "e";   int num = appearNumber(srcText, findText);   System.out.println(num); }  /**  * 获取指定字符串出现的次数  *  * @param srcText 源字符串  * @param findText 要查找的字符串  * @return  */ public static int appearNumber(String srcText, String findText) {   int count = 0;   Pattern p = Pattern.compile(findText);   Matcher m = p.matcher(srcText);   while (m.find()) {     count++;   }   return count; } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持VeVb武林网。


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表