Here you can find the source of splitIntoWords(String s)
Parameter | Description |
---|---|
s | a parameter |
public static List<String> splitIntoWords(String s)
//package com.java2s; /*//from www. j a v a2s . c o m * Copyright (C) 2012 The Stanford MobiSocial Laboratory * * 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.*; public class Main { /** * splits the input string into words e.g. * if the input is "a b c", return a list containing "a" "b" and "c" * if input is "a" a list containing just "a" is returned. * input must have at least one token. * * @param s * @return */ public static List<String> splitIntoWords(String s) { List<String> result = new ArrayList<String>(); if (s == null) return result; StringTokenizer st = new StringTokenizer(s); while (st.hasMoreTokens()) { String term = st.nextToken(); if (term.startsWith("\"")) { term = term.substring(1); // skip the leading " while (st.hasMoreTokens()) { term += " " + st.nextToken(); if (term.endsWith("\"")) { term = term.substring(0, term.length() - 1); // skip the trailing " break; } } } result.add(term); } return result; } }