码迷,mamicode.com
首页 > 编程语言 > 详细

java 实现统计某段文字在内容中出现的次数

时间:2014-11-08 11:41:14      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:android   http   io   ar   java   sp   strong   div   on   

http://outofmemory.cn/code-snippet/815/java-zishutongji
一个api,位于apache.commons.lang.StringUtils类下的一个StringUtils.countMatches(paragraph, string);方法,这个方法返回值是一个数字
 
 
代码:

package cn.outofmemory;

import org.apache.commons.lang3.StringUtils;

public class WordCounter {

    public static void main(String[] args) {

        // String with our paragraph
        String paragraph = "Java Code Geeks (JCGs) is an independent online community focused on creating the"
        + "ultimate Java-to-Java developers resource center; targeted at the technical architect, technical"
        + "team lead (senior developer), project manager and junior developers alike. JCGs serve the Java, "
        + "Scala, Android, SOA, Agile and Telecom communities with daily news written by domain experts, "
        + "articles, tutorials, reviews, announcements, code snippets and open source projects.";

        // Declare the word you want to search
        String string = "Java";

        // Count word repetitions
        int counter = StringUtils.countMatches(paragraph, string);

        // Print the result
        System.out.println("Word <" + string + "> appeared " + counter + " times in the paragraph.");
    }
}

 
 
 

java 实现统计某段文字在内容中出现的次数

标签:android   http   io   ar   java   sp   strong   div   on   

原文地址:http://www.cnblogs.com/svennee/p/4082827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!