Java String Split by Line splitString(String line)

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

Description

Splits a String into a String[].

License

Open Source License

Declaration

public static String[] splitString(String line) 

Method Source Code

//package com.java2s;
/*/*w  w  w  .  j  ava2 s  .  c  o m*/
 * aocode-public - Reusable Java library of general tools with minimal external dependencies.
 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2018, 2019  AO Industries, Inc.
 *     support@aoindustries.com
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aocode-public.
 *
 * aocode-public is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aocode-public is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aocode-public.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

public class Main {
    /**
     * Splits a <code>String</code> into a <code>String[]</code>.
     */
    public static String[] splitString(String line) {
        int len = line.length();
        int wordCount = 0;
        int pos = 0;
        while (pos < len) {
            // Skip past blank space
            while (pos < len && line.charAt(pos) <= ' ')
                pos++;
            int start = pos;
            // Skip to the next blank space
            while (pos < len && line.charAt(pos) > ' ')
                pos++;
            if (pos > start)
                wordCount++;
        }

        String[] words = new String[wordCount];

        int wordPos = 0;
        pos = 0;
        while (pos < len) {
            // Skip past blank space
            while (pos < len && line.charAt(pos) <= ' ')
                pos++;
            int start = pos;
            // Skip to the next blank space
            while (pos < len && line.charAt(pos) > ' ')
                pos++;
            if (pos > start)
                words[wordPos++] = line.substring(start, pos);
        }

        return words;
    }

    /**
     * Splits a <code>String</code> into a <code>String[]</code>.
     */
    public static int splitString(String line, char[][][] buff) {
        int len = line.length();
        int wordCount = 0;
        int pos = 0;
        while (pos < len) {
            // Skip past blank space
            while (pos < len && line.charAt(pos) <= ' ')
                pos++;
            int start = pos;
            // Skip to the next blank space
            while (pos < len && line.charAt(pos) > ' ')
                pos++;
            if (pos > start)
                wordCount++;
        }

        char[][] words = buff[0];
        if (words == null || words.length < wordCount)
            buff[0] = words = new char[wordCount][];

        int wordPos = 0;
        pos = 0;
        while (pos < len) {
            // Skip past blank space
            while (pos < len && line.charAt(pos) <= ' ')
                pos++;
            int start = pos;
            // Skip to the next blank space
            while (pos < len && line.charAt(pos) > ' ')
                pos++;
            if (pos > start) {
                int chlen = pos - start;
                char[] tch = words[wordPos++] = new char[chlen];
                System.arraycopy(line.toCharArray(), start, tch, 0, chlen);
            }
        }

        return wordCount;
    }

    /**
     * Splits a <code>String</code> into a <code>String[]</code>.
     */
    public static int splitString(String line, String[][] buff) {
        int len = line.length();
        int wordCount = 0;
        int pos = 0;
        while (pos < len) {
            // Skip past blank space
            while (pos < len && line.charAt(pos) <= ' ')
                pos++;
            int start = pos;
            // Skip to the next blank space
            while (pos < len && line.charAt(pos) > ' ')
                pos++;
            if (pos > start)
                wordCount++;
        }

        String[] words = buff[0];
        if (words == null || words.length < wordCount)
            buff[0] = words = new String[wordCount];

        int wordPos = 0;
        pos = 0;
        while (pos < len) {
            // Skip past blank space
            while (pos < len && line.charAt(pos) <= ' ')
                pos++;
            int start = pos;
            // Skip to the next blank space
            while (pos < len && line.charAt(pos) > ' ')
                pos++;
            if (pos > start)
                words[wordPos++] = line.substring(start, pos);
        }

        return wordCount;
    }

    /**
     * Splits a string on the given delimiter.
     * Does include all empty elements on the split.
     *
     * @return  the modifiable list from the split
     */
    public static List<String> splitString(String line, char delim) {
        return splitString(line, 0, line.length(), delim, new ArrayList<String>());
    }

    /**
     * Splits a string on the given delimiter.
     * Does include all empty elements on the split.
     *
     * @param  words  the words will be added to this collection.
     *
     * @return  the collection provided in words parameter
     */
    public static <C extends Collection<String>> C splitString(String line, char delim, C words) {
        return splitString(line, 0, line.length(), delim, words);
    }

    /**
     * Splits a string on the given delimiter over the given range.
     * Does include all empty elements on the split.
     *
     * @return  the modifiable list from the split
     */
    public static List<String> splitString(String line, int begin, int end, char delim) {
        return splitString(line, begin, end, delim, new ArrayList<String>());
    }

    /**
     * Splits a string on the given delimiter over the given range.
     * Does include all empty elements on the split.
     *
     * @param  words  the words will be added to this collection.
     *
     * @return  the collection provided in words parameter
     */
    public static <C extends Collection<String>> C splitString(String line, int begin, int end, char delim,
            C words) {
        int pos = begin;
        while (pos < end) {
            int start = pos;
            pos = line.indexOf(delim, pos);
            if (pos == -1 || pos > end)
                pos = end;
            words.add(line.substring(start, pos));
            pos++;
        }
        // If ending in a delimeter, add the empty string
        if (end > begin && line.charAt(end - 1) == delim)
            words.add("");
        return words;
    }

    public static List<String> splitString(String line, String delim) {
        int delimLen = delim.length();
        if (delimLen == 0)
            throw new IllegalArgumentException("Delimiter may not be empty");
        List<String> words = new ArrayList<>();
        int len = line.length();
        int pos = 0;
        while (pos < len) {
            int start = pos;
            pos = line.indexOf(delim, pos);
            if (pos == -1) {
                words.add(line.substring(start, len));
                pos = len;
            } else {
                words.add(line.substring(start, pos));
                pos += delimLen;
            }
        }
        // If ending in a delimeter, add the empty string
        if (len >= delimLen && line.endsWith(delim))
            words.add("");

        return words;
    }

    /**
     * Finds the first occurrence of any of the supplied characters
     *
     * @param  S  the <code>String</code> to search
     * @param  chars  the characters to look for
     *
     * @return  the index of the first occurrence of <code>-1</code> if none found
     */
    public static int indexOf(String S, char[] chars) {
        return indexOf(S, chars, 0);
    }

    /**
     * Finds the first occurrence of any of the supplied characters starting at the specified index.
     *
     * @param  S  the <code>String</code> to search
     * @param  chars  the characters to look for
     * @param  start  the starting index.
     *
     * @return  the index of the first occurrence of <code>-1</code> if none found
     */
    public static int indexOf(String S, char[] chars, int start) {
        int Slen = S.length();
        int clen = chars.length;
        for (int c = start; c < Slen; c++) {
            char ch = S.charAt(c);
            for (int d = 0; d < clen; d++)
                if (ch == chars[d])
                    return c;
        }
        return -1;
    }

    /**
     * Finds the next of a substring like regular String.indexOf, but stops at a certain maximum index.
     * Like substring, will look up to the character one before toIndex.
     */
    public static int indexOf(String source, String target, int fromIndex, int toIndex) {
        if (fromIndex > toIndex)
            throw new IllegalArgumentException(
                    "fromIndex>toIndex: fromIndex=" + fromIndex + ", toIndex=" + toIndex);

        int sourceCount = source.length();

        // This line makes it different than regular String indexOf method.
        if (toIndex < sourceCount)
            sourceCount = toIndex;

        int targetCount = target.length();

        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetCount == 0) {
            return fromIndex;
        }

        char first = target.charAt(0);
        int max = sourceCount - targetCount;

        for (int i = fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source.charAt(i) != first) {
                while (++i <= max && source.charAt(i) != first) {
                    // Intentionally empty
                }
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = 1; j < end && source.charAt(j) == target.charAt(k); j++, k++) {
                    // Intentionally empty
                }

                if (j == end) {
                    /* Found whole string. */
                    return i;
                }
            }
        }
        return -1;
    }
}

Related

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