Java String Indent Format indent(final StringBuffer buffer, final String text, final int indent)

Here you can find the source of indent(final StringBuffer buffer, final String text, final int indent)

Description

Appends the given (multi-line) text to the buffer with the given indent count.

License

Apache License

Parameter

Parameter Description
buffer buffer to append
text multi-line text to append
indent number of times to append the 2 spaces indentation

Declaration

public static void indent(final StringBuffer buffer, final String text, final int indent) 

Method Source Code

//package com.java2s;
/*/*  ww w .  ja  va  2 s  . c  o m*/
 * Copyright Hilbrand Bouwkamp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

public class Main {
    private static final String NL_S = "\n";
    private static final char NL = '\n';

    /**
     * Appends the given (multi-line) text to the buffer with the given indent
     * count. The text is split by new lines. So each line is appended with the
     * given indent.
     * @param buffer buffer to append
     * @param text multi-line text to append
     * @param indent number of times to append the 2 spaces indentation
     */
    public static void indent(final StringBuffer buffer, final String text, final int indent) {
        if (text != null) {
            for (final String row : text.split(NL_S)) {
                indent(buffer, indent);
                buffer.append(row);
                nl(buffer);
            }
            ;
        }
    }

    /**
     * Appends times the indent 2 spaces indentation to the buffer.
     * @param buffer buffer to append
     * @param indent number of times to append the 2 spaces indentation
     */
    public static void indent(final StringBuffer buffer, final int indent) {
        buffer.append(new String(new char[indent * 2]).replace('\0', ' '));
    }

    /**
     * Appends a new line to the buffer.
     * @param buffer buffer to append
     */
    public static void nl(final StringBuffer buffer) {
        buffer.append(NL);
    }

    /**
     * Appends the times number count a new line to the buffer.
     * @param buffer buffer to append
     * @param count number of times to append new line
     */
    public static void nl(final StringBuffer buffer, final int count) {
        buffer.append(new String(new char[count]).replace('\0', NL));
    }
}

Related

  1. indent(final int indent)
  2. indent(final int indents)
  3. indent(final int size)
  4. indent(final String s)
  5. indent(final String string, final String separator)
  6. indent(final StringBuilder aSb, final String aIndentString)
  7. indent(final StringBuilder buf, int level)
  8. indent(final StringBuilder s, final int depth)
  9. indent(final StringBuilder sb, final int indent)