Java String Split by Space splitStringOnWhitespace(String text)

Here you can find the source of splitStringOnWhitespace(String text)

Description

Split the given String on whitespace characters.

License

Open Source License

Parameter

Parameter Description
text The source String

Return

A List of String objects

Declaration


public static List splitStringOnWhitespace(String text) 

Method Source Code


//package com.java2s;
import java.util.List;

import java.util.ArrayList;

public class Main {
    /** Split the given String on whitespace characters.
        //w w w .  ja  v  a  2  s  .c o m
    @param text The source String
    @return A List of String objects
    */

    public static List splitStringOnWhitespace(String text) {
        List elements = new ArrayList();
        StringBuffer buffer = new StringBuffer();
        char[] chars = text.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (Character.isWhitespace(chars[i])) {
                if (buffer.length() > 0) {
                    elements.add(buffer.toString());
                    buffer = new StringBuffer();
                }
            } else {
                buffer.append(chars[i]);
            }
        }

        if (buffer.length() > 0) {
            elements.add(buffer.toString());
        }

        return elements;
    }
}

Related

  1. splitNamespaceTitle(String fullTitle)
  2. splitOnSpace(final String string)
  3. splitOnSpace(String string)
  4. SplitOnWhitespace(String instrData)
  5. splitSpaces(String input)
  6. splitWithoutWhitespace(String source, String separator)
  7. whitespaceSplit(final String input)