Here you can find the source of countWords(String segment, char[] wordDelimiters)
Parameter | Description |
---|---|
segment | a parameter |
wordDelimiters | a parameter |
public static int countWords(String segment, char[] wordDelimiters)
//package com.java2s; //License from project: Creative Commons License public class Main { /**/*from w ww. ja v a2s. co m*/ * Count Words * * @param segment * @param wordDelimiters * @return */ public static int countWords(String segment, char[] wordDelimiters) { int wordCount = 0; boolean lastWasGap = true; for (int i = 0; i < segment.length(); i++) { char ch = segment.charAt(i); boolean isDelimiter = false; for (int j = 0, isize = wordDelimiters.length; j < isize; j++) { if (ch == wordDelimiters[j]) { isDelimiter = true; lastWasGap = true; } } if (!isDelimiter && lastWasGap) { lastWasGap = false; wordCount++; } } return wordCount; } }