Java String Remove splitLine(String s, boolean removeNewLine)

Here you can find the source of splitLine(String s, boolean removeNewLine)

Description

split Line

License

Open Source License

Declaration

public static String[] splitLine(String s, boolean removeNewLine) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014,2015 Hideki Yatomi
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 ******************************************************************************/

import java.util.*;

public class Main {
    public static String[] splitLine(String s, boolean removeNewLine) {
        s = normalizeNewLine(s);/*from  w w  w  .j a  v  a2  s  .  c o m*/

        StringTokenizer tk = new StringTokenizer(s, "\n", true);
        String[] array = new String[tk.countTokens()];
        for (int i = 0; i < array.length; i++) {
            array[i] = tk.nextToken();
        }

        List<String> list = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            String e = array[i];
            if (i < array.length - 1 && !e.equals("\n")) {
                String next = array[i + 1];
                if (next.equals("\n")) {
                    e += next;
                    i++;
                }
            }

            if (removeNewLine)
                e = e.replace("\n", "");

            list.add(e);
        }
        return list.toArray(new String[list.size()]);
    }

    public static String normalizeNewLine(String s) {
        s = s.replace("\r\n", "\n");
        s = s.replace("\r", "\n");
        return s;
    }
}

Related

  1. removeSpecialChars(final String word)
  2. removeSpecialCharsForSQLRegExp(String _s)
  3. removeUnusedData(String songData)
  4. removeWaitStatus(String nodeInfo)
  5. removeWhitespace(String inputString)
  6. trimIndent(String line, int indentsToRemove, int tabWidth, int indentWidth)