Java Utililty Methods String Indent Format

List of utility methods to do String Indent Format

Description

The list of methods to do String Indent Format are organized into topic(s).

Method

Stringindent(String string, int indentSize, boolean initialLine)
indent Indent a String with line-breaks.
String indent;
if (indentSize == 0) {
    indent = "\t";
} else {
    StringBuffer s = new StringBuffer();
    for (int i = 0; i < indentSize; i++) {
        s.append(' ');
    indent = s.toString();
StringBuffer result = new StringBuffer();
if (initialLine) {
    result.append(indent);
for (int i = 0; i < string.length(); i++) {
    char c = string.charAt(i);
    result.append(c);
    if (c == '\n') {
        result.append(indent);
return result.toString();
Stringindent(String string, int level, boolean indentFirst, int tabSize)
Split the given string on each newline or line return, then indent all lines by tabSize for each level.
String indention = indention(level, tabSize);
String[] lines = string.split("\\r?\\n");
String answer = "";
for (int i = 0; i < lines.length; i++) {
    String l = lines[i];
    if (i == 0) {
        if (indentFirst) {
            answer += indention + l;
...
voidindent(String symbol, StringBuffer res, int indent, boolean comma)
indent
if (symbol.isEmpty()) {
    return;
for (int i = 0; i < indent; i++) {
    res.append("\t");
res.append(symbol);
if (comma) {
...
Stringindent(String text, String indentation, char indentationChar, int indentationLevel, int indentationSize, boolean requiresExtraIndentationOnFirstLine)
indent
String result = text;
StringBuilder sb = new StringBuilder();
String[] lines = text.split("\n");
String theoricalIndentationText = lines[0].substring(0, indentationSize * indentationLevel);
if (requiresExtraIndentationOnFirstLine) {
    for (int i = 0; i < indentationSize; i++) {
        sb.append(indentationChar);
sb.append(lines[0].substring(indentationSize * indentationLevel));
for (int i = 1; i < lines.length; i++) {
    String line = lines[i];
    if (indentation.length() > 0) {
        line = line.replaceFirst(theoricalIndentationText, indentation);
    sb.append('\n').append(line);
if (text.endsWith("\n")) {
    sb.append('\n');
result = sb.toString();
return result;
Stringindent(String value, int spaces)
Indents all lines of the argument string by the specified number of spaces.
if (spaces < 0)
    spaces = 0;
String indent = "";
for (int i = 0; i < spaces; i++) {
    indent += " ";
String[] lines = value.split("\r\n|\r|\n");
String output = "";
...
StringBufferindent(StringBuffer buffer, int depth)
indent
if (depth <= 0) {
    return buffer;
for (int i = 0; i < depth; i++) {
    buffer.append("  ");
return buffer;
voidindent(StringBuffer sb, int indentLevel)
indent
for (int i = 0; i < indentLevel; i++) {
    sb.append(INDENT_STRING);
voidindent(StringBuilder b, int i)
indent
while (i-- > 0)
    b.append(" "); 
StringBuilderindent(StringBuilder buffer, int d)
Appends the specified number of space charcter to a string buffer and returns it.
for (int i = 0; i < d; i++) {
    buffer.append(' ');
return buffer;
StringBuilderindent(StringBuilder builder, int indent)
Adds indent to builder.
while (0 < indent) {
    builder.append(INDENT);
    indent--;
return builder;