Here you can find the source of splitWords(String text)
public static List<String> splitWords(String text)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.Deque; import java.util.List; public class Main { public static List<String> splitWords(String text) { List<String> result = new ArrayList<>(); char[] chars = text.toCharArray(); int begin = 0; boolean word = false; for (int i = 0; i < chars.length; i++) { if (Character.isWhitespace(chars[i])) { if (word) { result.add(new String(chars, begin, i - begin)); word = false;/*from ww w . ja v a 2s .c o m*/ } } else { if (!word) { begin = i; word = true; } } } if (word) { result.add(new String(chars, begin, chars.length - begin)); } return result; } public static void splitWords(String text, Deque deque) { if (text != null && text.length() > 0) { char[] chars = text.toCharArray(); int index = 0; while (index < chars.length) { index += leftTrim(chars, index); String word = getNextWord(chars, index); if (word.length() > 0) { deque.addLast(word); index += word.length(); } else { return; } } } } /** * Return number of whitespace character from the offset to the first non * whitespace character. * * @param text * the scanned text * * @param offset * first character to inspect * * @return number of whitespaces or zero */ public static int leftTrim(char[] text, int offset) { int counter = 0; if (text != null && offset < text.length) { while (Character.isWhitespace(text[offset + counter])) { counter++; } } return counter; } public static String getNextWord(char[] text, int offset) { if (text != null && offset < text.length) { boolean word = false; for (int i = offset; i < text.length; i++) { if (Character.isWhitespace(text[i])) { if (word) { return new String(text, offset, i - offset); } } else { word = true; } } if (word) { return new String(text, offset, text.length - offset); } } return ""; } }