Indents every line of the String by 1 tab. - Java java.lang

Java examples for java.lang:String Format

Description

Indents every line of the String by 1 tab.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String str = "java2s.com";
        System.out.println(indent(str));
    }// w ww .ja v  a2 s . c om

    /**
     * Indents every line of the String by 1 tab.
     * 
     * @param str
     *            The string to indent
     * @return The indented string
     */
    public static String indent(String str) {
        String[] lines = str.split("\\n");
        String newStr = "";
        for (String line : lines) {
            newStr += "\t" + line + "\n";
        }
        return newStr;
    }
}

Related Tutorials