Java String Indent Format indentStringBuffer(StringBuffer sb, int indent)

Here you can find the source of indentStringBuffer(StringBuffer sb, int indent)

Description

add indent to a StringBuffer

License

Apache License

Parameter

Parameter Description
sb non-null StringBuffer
indent non-negative indent

Declaration

public static void indentStringBuffer(StringBuffer sb, int indent) 

Method Source Code

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

public class Main {
    private static String[] gIndentStrings;

    /**/*from  ww  w.ja  v a  2 s .c o  m*/
     * add indent to a StringBuffer
     * @param sb  non-null  StringBuffer
     * @param indent non-negative indent
     */
    public static void indentStringBuffer(StringBuffer sb, int indent) {
        sb.append(indentString(indent));

    }

    /**
     *     build indent String
     *     @param indent non-negative every level indents 4 spaces
     *     @return string with indented CR
     */
    public static String indentString(int indent) {
        if (gIndentStrings == null) {
            buildIndentStrings();
        }
        if (indent < gIndentStrings.length)
            return (gIndentStrings[indent]);
        // too long so build
        StringBuffer sb = new StringBuffer(4 * indent);
        for (int i = 0; i < indent; i++)
            sb.append("    ");
        return (sb.toString());

    }

    /**
     *     build indent String
     *     @return string with indented CR
     */
    protected static synchronized void buildIndentStrings() {
        if (gIndentStrings != null) {
            return;
        }
        String[] TheStrings = new String[16];
        String IndentString = "";
        for (int i = 0; i < TheStrings.length; i++) {
            TheStrings[i] = IndentString;
            IndentString += "    ";
        }
        gIndentStrings = TheStrings;
    }

    /**{ method
     @name toString
     @function convert a char to a string
     @param c the char
     @return the String
     }*/
    public static String toString(char c) {
        StringBuffer s = new StringBuffer();
        s.append(c);
        return (s.toString());
    }
}

Related

  1. indentString(final int indentSpaces)
  2. indentString(int indent)
  3. indentString(int indent)
  4. indentString(String s, char open, char middle, char close)
  5. indentString(String str, String indent)
  6. indentText(String text, boolean indentFirstLine)
  7. indentTransform(String in, int indent)
  8. indentWith(String s)