Java String Split by Line splitNextWord(String line)

Here you can find the source of splitNextWord(String line)

Description

split Next Word

License

Open Source License

Declaration

public static List<String> splitNextWord(String line) 

Method Source Code

//package com.java2s;
/*/*from   w  w w.j av a2  s  .  c o m*/
 * This software copyright by various authors including the RPTools.net
 * development team, and licensed under the LGPL Version 3 or, at your option,
 * any later version.
 * 
 * Portions of this software were originally covered under the Apache Software
 * License, Version 1.1 or Version 2.0.
 * 
 * See the file LICENSE elsewhere in this distribution for license details.
 */

import java.util.Arrays;
import java.util.List;

public class Main {
    public static List<String> splitNextWord(String line) {
        line = line.trim();
        if (line.length() == 0) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        boolean quoted = line.charAt(0) == '"';

        int start = quoted ? 1 : 0;
        int end = start;
        for (; end < line.length(); end++) {
            char c = line.charAt(end);
            if (quoted) {
                if (c == '"') {
                    break;
                }
            } else {
                if (Character.isWhitespace(c)) {
                    break;
                }
            }
            builder.append(c);
        }
        return Arrays.asList(
                new String[] { line.substring(start, end), line.substring(Math.min(end + 1, line.length())) });
    }
}

Related

  1. splitLines(String text)
  2. splitLines(String text)
  3. splitLogLine(final String logLine)
  4. splitMultiline(String text, boolean trimLines)
  5. splitNewLines(final String input)
  6. splitString(String line)
  7. splitStringPerWord(String string, int wordsPerLine)
  8. splitter(String line, char delimeter)
  9. splitToLines(CharSequence str)