Java String Indent Format indentTransform(String in, int indent)

Here you can find the source of indentTransform(String in, int indent)

Description

add indent to a String

License

Apache License

Parameter

Parameter Description
out non-null PrintStream
level non-negative every level indents 4 spaces

Return

string with indented CR

Declaration

public static String indentTransform(String in, int indent) 

Method Source Code

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

public class Main {
    private static String[] gIndentStrings;

    /**//  w  w w. j a v a  2s.  c  o  m
     *     add indent to a String
     *     @param out non-null PrintStream
     *     @param level non-negative every level indents 4 spaces
     *     @return string with indented CR
     */
    public static String indentTransform(String in, int indent) {
        StringBuffer sb = new StringBuffer(in.length() + 64 * indent);
        String IndentString = indentString(indent);
        for (int i = 0; i < in.length(); i++) {
            char c = in.charAt(i);
            switch (c) {
            case '\n':
                sb.append(c);
                if (i < (in.length() - 1))
                    sb.append(IndentString);
                break;
            case '\t':
                sb.append("    ");
                break;
            default:
                if (c >= ' ')
                    sb.append(c);
            }
        }
        return (sb.toString());
    }

    /**
     *     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());

    }

    /**{ 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());
    }

    /**
     *     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;
    }
}

Related

  1. indentString(int indent)
  2. indentString(String s, char open, char middle, char close)
  3. indentString(String str, String indent)
  4. indentStringBuffer(StringBuffer sb, int indent)
  5. indentText(String text, boolean indentFirstLine)
  6. indentWith(String s)