Java String Indent Format indent(String input, int depth, int start)

Here you can find the source of indent(String input, int depth, int start)

Description

Helper method used to indent text.

License

Open Source License

Parameter

Parameter Description
input The text to be indented.
depth Depth of the indentation.
start Number of lines after which the indentation should start.

Return

The text with indentation.

Declaration

public static String indent(String input, int depth, int start) 

Method Source Code

//package com.java2s;
/*/*  w w  w  . ja va  2s .  c  om*/
 * Filename: CLIHelper.java
 * 
 * Copyright 2015 Jean-Pierre Hoehmann (jeanpierre.hoehmann@gmail.com)
 *
 * This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hovecar. See 
 * http://www.wtfpl.net/ for more details.
 */

public class Main {
    /**
     * Helper method used to indent text.
     *
     * @param input
     *            The text to be indented.
     * @param depth
     *            Depth of the indentation.
     * @param start
     *            Number of lines after which the indentation should start.
     * @return The text with indentation.
     */
    public static String indent(String input, int depth, int start) {

        String[] text = stringToArray(input);

        // Simply iterate from start to end and append the given number of spaces.
        for (int i = start; i < text.length; i++) {
            text[i] = printNTimes(" ", depth) + text[i];
        }

        return arrayToString(text);
    }

    /**
     * Helper method used to convert a multiline String into an Array of singleline Strings.
     *
     * @param input
     *            Multiline String to be turned into an Array.
     * @return An Array of Strings with each String representating a single line.
     */
    public static String[] stringToArray(String input) {

        // Simply split the input String at all the newlines.
        // Two Backslashes here because of Regex.
        // This version of split automatically removes the trailing empty String.
        return input.split(System.lineSeparator());
    }

    /**
     * Helper method for easily printing a String multiple times into a single line.
     *
     * @param input
     *            String to be printed.
     * @param n
     *            Times the String is to be printed.
     * @return The input String repeated n times.
     */
    public static String printNTimes(String input, int n) {

        String output = "";

        // Print the input String n times into output.
        for (int i = 0; i < n; i++) {
            output += input;
        }

        return output;
    }

    /**
     * Helper method used to convert an Array of Strings into one multiline String.
     *
     * @param input
     *            The Array of Strings to be converted.
     * @return The converted String.
     */
    public static String arrayToString(String[] input) {

        String output = "";

        // Iterate over the array and append each line including a line feed to the output.
        // This will produce a String with a trailing newline.
        // Restoration of the original String is not possible,
        // because stringToArray simply strips trailing newlines for easier formatting.
        for (int i = 0; i < input.length; i++) {
            output += input[i];
            output += System.lineSeparator();
        }

        return output;
    }
}

Related

  1. indent(int numSpaces)
  2. indent(int numSpaces)
  3. indent(int tabCount)
  4. indent(String in)
  5. indent(String in)
  6. indent(String lines, String indentation)
  7. indent(String message, int indent)
  8. indent(String msg, int size)
  9. indent(String original, int spaces)