Java String Indent Format indent(String string, int level, boolean indentFirst, int tabSize)

Here you can find the source of indent(String string, int level, boolean indentFirst, int tabSize)

Description

Split the given string on each newline or line return, then indent all lines by tabSize for each level.

License

Open Source License

Declaration

public static String indent(String string, int level, boolean indentFirst, int tabSize) 

Method Source Code

//package com.java2s;
/*//  ww w  .j  a  v  a 2  s.  c  o  m
 * Copyright (c) 2016 Catavolt Inc. All rights reserved.
 *
 * This file is part of Ozy.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static final int DEFAULT_TAB_SIZE = 4;

    public static String indent(String string, int level) {
        return indent(string, level, true, DEFAULT_TAB_SIZE);
    }

    public static String indent(String string, int level, boolean indentFirst) {
        return indent(string, level, indentFirst, DEFAULT_TAB_SIZE);
    }

    /**
     * Split the given string on each newline or line return, then indent all lines by tabSize for each level.
     * There is no indent for level zero.
     */
    public static String indent(String string, int level, boolean indentFirst, int tabSize) {
        String indention = indention(level, tabSize);
        String[] lines = string.split("\\r?\\n");
        String answer = "";
        for (int i = 0; i < lines.length; i++) {
            String l = lines[i];
            if (i == 0) {
                if (indentFirst) {
                    answer += indention + l;
                } else {
                    answer += l;
                }
            } else {
                answer += "\n" + indention + l;
            }
        }
        return answer;
    }

    /**
     * Create an indention string that contains level * tabSize space characters.
     */
    public static String indention(int level, int tabSize) {
        StringBuilder indention = new StringBuilder();
        for (int i = 0; i < level * tabSize; i++) {
            indention.append(" ");
        }
        return indention.toString();
    }

    public static String indention(int level) {
        return indention(level, DEFAULT_TAB_SIZE);
    }
}

Related

  1. indent(String s, int numBlanks)
  2. indent(String str)
  3. indent(String str, String indent)
  4. indent(String string)
  5. indent(String string, int indentSize, boolean initialLine)
  6. indent(String symbol, StringBuffer res, int indent, boolean comma)
  7. indent(String text, String indentation, char indentationChar, int indentationLevel, int indentationSize, boolean requiresExtraIndentationOnFirstLine)
  8. indent(String value, int spaces)
  9. indent(StringBuffer buffer, int depth)