Java String Indent Format indent(String text, String indentation, char indentationChar, int indentationLevel, int indentationSize, boolean requiresExtraIndentationOnFirstLine)

Here you can find the source of indent(String text, String indentation, char indentationChar, int indentationLevel, int indentationSize, boolean requiresExtraIndentationOnFirstLine)

Description

indent

License

LGPL

Declaration

public static String indent(String text, String indentation, char indentationChar, int indentationLevel,
            int indentationSize, boolean requiresExtraIndentationOnFirstLine) 

Method Source Code

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

public class Main {
    public static String indent(String text, String indentation, char indentationChar, int indentationLevel,
            int indentationSize, boolean requiresExtraIndentationOnFirstLine) {

        String result = text;//  w w  w  . j  a  va2  s  .  co  m

        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) {
                //we replace the supposed indentation chars at indentation level for the existing ones.
                line = line.replaceFirst(theoricalIndentationText, indentation);
            }

            sb.append('\n').append(line);
        }

        if (text.endsWith("\n")) {
            sb.append('\n');
        }
        result = sb.toString();

        return result;
    }
}

Related

  1. indent(String str, String indent)
  2. indent(String string)
  3. indent(String string, int indentSize, boolean initialLine)
  4. indent(String string, int level, boolean indentFirst, int tabSize)
  5. indent(String symbol, StringBuffer res, int indent, boolean comma)
  6. indent(String value, int spaces)
  7. indent(StringBuffer buffer, int depth)
  8. indent(StringBuffer sb, int indentLevel)
  9. indent(StringBuilder b, int i)