Java String Extract extractLines(String data, int tabWidth)

Here you can find the source of extractLines(String data, int tabWidth)

Description

Extracts lines of text from the specified data.

License

Open Source License

Parameter

Parameter Description
data The text to extract lines from.
tabWidth The width (in spaces) of a tab character. Pass in <code>0</code> or less to leave tab characters in place.

Return

The lines of text.

Declaration

public static final ArrayList<String> extractLines(String data, int tabWidth) 

Method Source Code


//package com.java2s;
/*/*from   w w w. j  a v a 2 s .  co  m*/
 * Copyright (c) 1998-2015 by Richard A. Wilkes. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * version 2.0. If a copy of the MPL was not distributed with this file, You
 * can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This Source Code Form is "Incompatible With Secondary Licenses", as defined
 * by the Mozilla Public License, version 2.0.
 */

import java.util.ArrayList;

public class Main {
    /**
     * Extracts lines of text from the specified data.
     *
     * @param data The text to extract lines from.
     * @param tabWidth The width (in spaces) of a tab character. Pass in <code>0</code> or less to
     *            leave tab characters in place.
     * @return The lines of text.
     */
    public static final ArrayList<String> extractLines(String data, int tabWidth) {
        int length = data.length();
        StringBuilder buffer = new StringBuilder(length);
        char ignoreCh = 0;
        ArrayList<String> lines = new ArrayList<>();
        int column = 0;

        for (int i = 0; i < length; i++) {
            char ch = data.charAt(i);

            if (ch == ignoreCh) {
                ignoreCh = 0;
            } else if (ch == '\r') {
                ignoreCh = '\n';
                column = 0;
                lines.add(buffer.toString());
                buffer.setLength(0);
            } else if (ch == '\n') {
                ignoreCh = '\r';
                column = 0;
                lines.add(buffer.toString());
                buffer.setLength(0);
            } else if (ch == '\t' && tabWidth > 0) {
                int spaces = tabWidth - column % tabWidth;

                ignoreCh = 0;
                while (--spaces >= 0) {
                    buffer.append(' ');
                    column++;
                }
            } else {
                ignoreCh = 0;
                column++;
                buffer.append(ch);
            }
        }
        if (buffer.length() > 0) {
            lines.add(buffer.toString());
        }

        return lines;
    }
}

Related

  1. extractGenericTypeNames(String sig)
  2. extractHorizontalTabs(String line, int tabSize)
  3. extractItems(String items)
  4. extractJobId(String line)
  5. extractKeyCodes(String codes)
  6. extractNoPublicDomains(String domains)
  7. extractNumberFromInput(String command)
  8. extractNumbers(String text)
  9. extractPacket(String serverPacket)