Java String Split by Word splitIntoWords(String s)

Here you can find the source of splitIntoWords(String s)

Description

splits the input string into words e.g.

License

Apache License

Parameter

Parameter Description
s a parameter

Declaration

public static List<String> splitIntoWords(String s) 

Method Source Code

//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;
    }
}

Related

  1. split(String words, String character)
  2. splitIdentifierToWords(String str)
  3. splitKeyWords(String sql)
  4. splitOne(String wordString)
  5. splitUnit(String word)
  6. splitWord(List words, int listIndex)