Java String Indent Format indent(String value, int spaces)

Here you can find the source of indent(String value, int spaces)

Description

Indents all lines of the argument string by the specified number of spaces.

License

Open Source License

Parameter

Parameter Description
value The string whose lines are to be indented
spaces The number of spaces by which to indent each line

Return

The resulting indented string. Each line will be terminated by a single newline character, regardless of the line terminators used in the original string.

Declaration

public static String indent(String value, int spaces) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   w w  w. j a v  a 2s . c o  m*/
     * Indents all lines of the argument string by the specified number of spaces.
     * @param value      The string whose lines are to be indented
     * @param spaces   The number of spaces by which to indent each line
     * @return         The resulting indented string.  Each line will be terminated by
     *                a single newline character, regardless of the line terminators
     *                used in the original string.
     */
    public static String indent(String value, int spaces) {
        if (spaces < 0)
            spaces = 0;
        String indent = "";
        for (int i = 0; i < spaces; i++) {
            indent += " ";
        }
        String[] lines = value.split("\r\n|\r|\n");
        String output = "";
        for (int i = 0; i < lines.length; i++) {
            output += indent + lines[i] + "\n";
        }
        return output;
    }
}

Related

  1. indent(String string)
  2. indent(String string, int indentSize, boolean initialLine)
  3. indent(String string, int level, boolean indentFirst, int tabSize)
  4. indent(String symbol, StringBuffer res, int indent, boolean comma)
  5. indent(String text, String indentation, char indentationChar, int indentationLevel, int indentationSize, boolean requiresExtraIndentationOnFirstLine)
  6. indent(StringBuffer buffer, int depth)
  7. indent(StringBuffer sb, int indentLevel)
  8. indent(StringBuilder b, int i)
  9. indent(StringBuilder buffer, int d)