Java String Split by Word splitwords(String s)

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

Description

Split a string into words separated by whitespace SPACE | TAB | NEWLINE | CR Citation chars '"' may be used to group words with embedded whitespace.

License

Open Source License

Declaration

public static String[] splitwords(String s) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**/*from w  ww  .  jav  a2s  .co m*/
     * Default citation char for splitwords(). Value is <tt>'"'</tt>
     */
    protected static char CITCHAR = '"';

    /**
     * Split a string into words separated by whitespace SPACE | TAB | NEWLINE |
     * CR
     * 
     * Citation chars '"' may be used to group words with embedded whitespace.
     * </p>
     */
    public static String[] splitwords(String s) {
        return splitwords(s, " \t\n\r", '"');
    }

    /**
     * Utility method to split a string into words separated by whitespace.
     * 
     * <p>
     * Equivalent to <tt>splitwords(s, WHITESPACE, CITCHAR)</tt>
     * </p>
     */
    public static String[] splitwords(String s, String whiteSpace) {
        return splitwords(s, whiteSpace, CITCHAR);
    }

    /**
     * Split a string into words separated by whitespace. Citation chars '"' may
     * be used to group words with embedded whitespace.
     * 
     * @param s
     *          String to split.
     * @param whiteSpace
     *          whitespace to use for splitting. Any of the characters in the
     *          whiteSpace string are considered whitespace between words and will
     *          be removed from the result.
     * @param citchar
     *          citation char used for enclosing words containing whitespace
     */
    public static String[] splitwords(String s, String whiteSpace, char citchar) {
        boolean bCit = false; // true when inside citation chars.
        Vector<String> v = new Vector<String>(); // (String) individual words after splitting
        StringBuffer buf = null;
        int i = 0;

        while (i < s.length()) {
            char c = s.charAt(i);

            if (bCit || whiteSpace.indexOf(c) == -1) {
                // Build up word until we breaks on
                // either a citation char or whitespace
                if (c == citchar) {
                    bCit = !bCit;
                } else {
                    if (buf == null) {
                        buf = new StringBuffer();
                    }
                    buf.append(c);
                }
                i++;
            } else {
                // found whitespace or end of citation, append word if we have one
                if (buf != null) {
                    v.addElement(buf.toString());
                    buf = null;
                }

                // and skip whitespace so we start clean on a word or citation char
                while ((i < s.length()) && (-1 != whiteSpace.indexOf(s.charAt(i)))) {
                    i++;
                }
            }
        }
        // Add possible remaining word
        if (buf != null) {
            v.addElement(buf.toString());
        }

        // Copy back into an array
        String[] r = new String[v.size()];
        v.copyInto(r);

        return r;
    }
}

Related

  1. splitKeyWords(String sql)
  2. splitOne(String wordString)
  3. splitUnit(String word)
  4. splitWord(List words, int listIndex)
  5. splitWords(String name)
  6. splitwords(String text)
  7. splitWords(String text, String splitter)