Here you can find the source of addAllWords(String text, List
private static void addAllWords(String text, List<String> result)
//package com.java2s; /*/*from w ww . j a v a 2s. c o m*/ * Copyright 2000-2009 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.List; public class Main { private static void addAllWords(String text, List<String> result) { int start = 0; while (start < text.length()) { int next = nextWord(text, start); result.add(text.substring(start, next)); start = next; } } static int nextWord(String text, int start) { if (!Character.isLetterOrDigit(text.charAt(start))) { return start + 1; } int i = start; boolean prevIsLetterOrDigit = i > 0 && Character.isLetterOrDigit(text.charAt(i - 1)); while (i < text.length()) { char c = text.charAt(i); if (!isWordStart(c)) { if (!Character.isLetterOrDigit(c)) { break; } if (prevIsLetterOrDigit) { break; } } prevIsLetterOrDigit = true; i++; } if (i > start + 1) { if (i == text.length() || !Character.isLetterOrDigit(text.charAt(i))) { return i; } return i - 1; } /*boolean */prevIsLetterOrDigit = i > 0 && Character.isLetterOrDigit(text.charAt(i - 1)); while (i < text.length()) { char c = text.charAt(i); if (!Character.isLetterOrDigit(c)) break; if (isWordStart(c)) break; if (!prevIsLetterOrDigit) break; prevIsLetterOrDigit = true; i++; } return i; } static boolean isWordStart(char p) { return Character.isUpperCase(p) || Character.isDigit(p); } }