how to add whitespace before a string in java

htt‮w//:sp‬ww.lautturi.com
how to add whitespace before a string in java
/**
 * @author lautturi.com
 * Java example: how to add whitespace before a string in java
 */

import java.util.*;

public class Lautturi {

	public static void main(String[] args) {
        
		// prepend 10 whitespaces before a string
		String s = "%sHello World!";
	    StringBuilder builder = new StringBuilder();
	    for(int i=0;i<10;i++){
	        builder.append(" ");
	    }

	    System.out.println(s.format(s,builder.toString())); 
	    
	 // prepend 10 whitespaces before a string
	    String str = "Hello World!";
	    for(int i=0;i<10;i++){
	    	str = " "+str;
	    }
	    System.out.println(str);
	    
	}
}

output:

Hello World!
          Hello World!
Created Time:2017-09-09 22:05:22  Author:lautturi