Java String Indent Format indentCode(String code, int indentLevel)

Here you can find the source of indentCode(String code, int indentLevel)

Description

Adds indentLevel spaces to every line of code.

License

Open Source License

Declaration

public static String indentCode(String code, int indentLevel) 

Method Source Code

//package com.java2s;
/*//from   w  ww . ja  v  a2 s .c  o  m
 * Copyright 2013 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Eclipse Public License version 1.0, available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Adds indentLevel spaces to every line of code.
     */
    public static String indentCode(String code, int indentLevel) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < indentLevel; i++) {
            sb.append(' ');
        }

        String indent = sb.toString();

        sb = new StringBuilder();
        String[] lines = code.split("\n");

        for (String line : lines) {
            sb.append(indent);
            sb.append(line);
            sb.append('\n');
        }

        return sb.toString();
    }
}

Related

  1. indentAllLines(String s, String indent)
  2. indentation(int howMany)
  3. indentation(int numberOfIndent)
  4. indentationLevel(CharSequence c)
  5. indentationStringOfCursorLine(String text, int cursor)
  6. indented(String indenter, String s)
  7. indentHtmlSpaces(int count)
  8. indention(int level, int tabSize)
  9. indentLinesAfterNth(final int skip, final int indent, final String str)