Java String Indent Format indentText(String text, boolean indentFirstLine)

Here you can find the source of indentText(String text, boolean indentFirstLine)

Description

Adds an indentation before every line (optionally the first line can be an exception) of the given string.

License

Apache License

Parameter

Parameter Description
text the possibly multi line text which is to be indented. This argument can be null , in which case the string "null" is used for this argument.
indentFirstLine true if the first line of the specified text must be indented, false if the first line must not be indented

Return

the specified text after the applied indentation. This method never returns null (not even if the specified text was null ).

Declaration

public static String indentText(String text, boolean indentFirstLine) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static final String INDENTATION = "  ";
    private static final String NULL_STR = "null";

    /**/*from  w  ww. j a v a  2 s. c  o  m*/
     * Adds an indentation before every line (optionally the first line can be
     * an exception) of the given string. The line separator is the {@code '\n'}
     * character (UNICODE: 000A).
     * <P>
     * The indentation is currently two space characters.
     *
     * @param text the possibly multi line text which is to be indented. This
     *   argument can be {@code null}, in which case the string {@code "null"}
     *   is used for this argument.
     * @param indentFirstLine {@code true} if the first line of the specified
     *   text must be indented, {@code false} if the first line must not be
     *   indented
     * @return the specified text after the applied indentation. This method
     *   never returns {@code null} (not even if the specified text was
     *   {@code null}).
     *
     * @see #toIndentedString(Object, boolean)
     */
    public static String indentText(String text, boolean indentFirstLine) {
        String result = text != null ? text.replace("\n", "\n" + INDENTATION) : NULL_STR;

        return indentFirstLine ? (INDENTATION + result) : result;
    }
}

Related

  1. indentString(int indent)
  2. indentString(int indent)
  3. indentString(String s, char open, char middle, char close)
  4. indentString(String str, String indent)
  5. indentStringBuffer(StringBuffer sb, int indent)
  6. indentTransform(String in, int indent)
  7. indentWith(String s)