Here you can find the source of indent(String s)
Parameter | Description |
---|---|
s | the string |
public static String indent(String s)
//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(); } }