Java implementation Statistics The number of occurrences of a piece of text in the content

Source: Internet
Author: User

Http://outofmemory.cn/code-snippet/815/java-zishutongji
An API, located under the Apache.commons.lang.StringUtils class, under a stringutils.countmatches (paragraph, string); method, the return value of this method is a numberCode: 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 a 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 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 implementation Statistics The number of occurrences of a piece of text in the content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.