Java String Indent Format indent(CharSequence input)

Here you can find the source of indent(CharSequence input)

Description

Indent a String.

License

Open Source License

Parameter

Parameter Description
input the input String

Return

the indented String. This returns StringBuilder as that is usually more efficient.

Declaration

public static StringBuilder indent(CharSequence input) 

Method Source Code

//package com.java2s;
// compliance with the InfoGrid license. The InfoGrid license and important

public class Main {
    /**//from ww  w .j  a  v a 2  s .  c o  m
     * Indent a String.
     *
     * @param input the input String
     * @return the indented String. This returns StringBuilder as that is usually more efficient.
     */
    public static StringBuilder indent(CharSequence input) {
        return indent(input, "    ", 1, 1);
    }

    /**
     * Indent a String.
     *
     * @param input the input String
     * @param indent the indent to use
     * @return the indented String. This returns StringBuilder as that is usually more efficient.
     */
    public static StringBuilder indent(CharSequence input, String indent) {
        return indent(input, indent, 1, 1);
    }

    /**
     * Indent a String.
     *
     * @param input the input String
     * @param indent the indent to use
     * @param firstLineN the number of indents to use for the first line
     * @param sunsequentN the number of indents to use for subsequent lines
     * @return the indented String. This returns StringBuilder as that is usually more efficient.
     */
    public static StringBuilder indent(CharSequence input, String indent, int firstLineN, int sunsequentN) {
        StringBuilder buf = new StringBuilder(input.length() + indent.length() * firstLineN); // one line is sure to fit

        for (int k = 0; k < firstLineN; ++k) {
            buf.append(indent);
        }
        for (int i = 0; i < input.length(); ++i) {
            char c = input.charAt(i);
            switch (c) {
            case '\n':
                buf.append(c);
                for (int k = 0; k < sunsequentN; ++k) {
                    buf.append(indent);
                }
                break;
            default:
                buf.append(c);
                break;
            }
        }
        return buf;
    }
}

Related

  1. indent()
  2. indent(char thr, int level)
  3. indent(final int depth, final int indentWidth, final StringBuilder buf)
  4. indent(final int i, StringBuffer b)
  5. indent(final int indent)
  6. indent(final int indents)