Java String Indent Format indent(String string, int indentSize, boolean initialLine)

Here you can find the source of indent(String string, int indentSize, boolean initialLine)

Description

indent Indent a String with line-breaks.

License

Open Source License

Parameter

Parameter Description
string String to indent.
indentSize Number of spaces to indent by. 0 will indent using a tab.
initialLine Whether to indent initial line.

Return

Indented string.

Declaration

public final static String indent(String string, int indentSize, boolean initialLine) 

Method Source Code

//package com.java2s;
/*/*  w  ww. j  a v a 2 s. co m*/
 * @(#)TextUtility.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */

public class Main {
    /**
     * indent
     *
     * Indent a String with line-breaks.
     *
     * @param string       String to indent.
     * @param indentSize   Number of spaces to indent by. 0 will indent using a tab.
     * @param initialLine  Whether to indent initial line.
     * @return             Indented string.
     */
    public final static String indent(String string, int indentSize, boolean initialLine) {
        // Create indent String
        String indent;

        if (indentSize == 0) {
            indent = "\t";
        } else {
            StringBuffer s = new StringBuffer();
            for (int i = 0; i < indentSize; i++) {
                s.append(' ');
            }
            indent = s.toString();
        }

        // Apply indent to input
        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();
    }
}

Related

  1. indent(String s)
  2. indent(String s, int numBlanks)
  3. indent(String str)
  4. indent(String str, String indent)
  5. indent(String string)
  6. indent(String string, int level, boolean indentFirst, int tabSize)
  7. indent(String symbol, StringBuffer res, int indent, boolean comma)
  8. indent(String text, String indentation, char indentationChar, int indentationLevel, int indentationSize, boolean requiresExtraIndentationOnFirstLine)
  9. indent(String value, int spaces)