Here you can find the source of indentTransform(String in, int indent)
Parameter | Description |
---|---|
out | non-null PrintStream |
level | non-negative every level indents 4 spaces |
public static String indentTransform(String in, int indent)
//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; } }