Java String Split splitText(String text)

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

Description

Split text into lines.

License

Apache License

Parameter

Parameter Description
text a text to split

Return

a list of elements (note that there are always at least one element)

Declaration

public static List<String> splitText(String text) 

Method Source Code


//package com.java2s;
/*/*w  w w .ja  v a2 s. co  m*/
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * 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.ArrayList;
import java.util.List;

public class Main {
    /**
     * Split text into lines. New line characters are treated as separators. So if the text starts
     * with newline, empty string will be the first element, if the text ends with new line, the
     * empty string will be the last element. The returned lines will be substrings of
     * the text argument. The new line characters are included into the line text.
     *
     * @param text a text to split
     * @return a list of elements (note that there are always at least one element)
     */
    public static List<String> splitText(String text) {
        int startLine = 0;
        int i = 0;
        int n = text.length();
        ArrayList<String> rc = new ArrayList<String>();
        while (i < n) {
            switch (text.charAt(i)) {
            case '\n':
                i++;
                if (i < n && text.charAt(i) == '\r') {
                    i++;
                }
                rc.add(text.substring(startLine, i));
                startLine = i;
                break;
            case '\r':
                i++;
                if (i < n && text.charAt(i) == '\n') {
                    i++;
                }
                rc.add(text.substring(startLine, i));
                startLine = i;
                break;
            default:
                i++;
            }
        }
        if (startLine == text.length()) {
            // still add empty line or previous line wouldn't be treated as completed
            rc.add("");
        } else {
            rc.add(text.substring(startLine, i));
        }
        return rc;
    }
}

Related

  1. splitTags(final String tagsString)
  2. splitTags(String rawTags)
  3. splitTemplateName(String templateName)
  4. splitter(List list, final int L)
  5. splitTerms(String queryString)
  6. splitToInteger(String str)
  7. splitTokens(final String targets)
  8. splitTokens(String text)
  9. splitToList(final String listString, final String split)