Java String Indent Format indent(String s)

Here you can find the source of indent(String s)

Description

Indents a string with 4 spaces.

License

Open Source License

Parameter

Parameter Description
s the string

Return

the indented string

Declaration

public static String indent(String s) 

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 a string with 4 spaces.
     *
     * @param s the string
     * @return the indented string
     */
    public static String indent(String s) {
        return indent(s, 4, true);
    }

    /**
     * Indents a string with spaces.
     *
     * @param s the string
     * @param spaces the number of spaces
     * @param newline append a newline if there is none
     * @return the indented string
     */
    public static String indent(String s, int spaces, boolean newline) {
        StringBuilder buff = new StringBuilder(s.length() + spaces);
        for (int i = 0; i < s.length();) {
            for (int j = 0; j < spaces; j++) {
                buff.append(' ');
            }
            int n = s.indexOf('\n', i);
            n = n < 0 ? s.length() : n + 1;
            buff.append(s.substring(i, n));
            i = n;
        }
        if (newline && !s.endsWith("\n")) {
            buff.append('\n');
        }
        return buff.toString();
    }
}

Related

  1. indent(String input, int depth, int start)
  2. indent(String lines, String indentation)
  3. indent(String message, int indent)
  4. indent(String msg, int size)
  5. indent(String original, int spaces)
  6. indent(String s)
  7. indent(String s)
  8. indent(String s)
  9. indent(String s, int numBlanks)