October 06, 2018

Java Example for Word Count

/**
 * Word count in java
 *
 * Using regular expression for split by space
 */
package com.belazy.algorithm;
/**
 *
 * @author Murali Krishnan
 *
 */
public class WordCount {

/**
* @param regex
*/
public static void main(String[] regex) {
String s = "I always value my    friends";
// split by single space
String[] sw = s.split("\\s");
// + indicates one or more
String[] wc = s.split("\\s+");
System.out.println(sw.length);
System.out.println(wc.length);
}
}

No comments:

Post a Comment

Your feedback may help others !!!

Facebook comments