Monday, August 8, 2016

Write a Program to Add spaces before and After a String using String.format()

Write a Program to Add spaces before and After a String using String.format()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
      package com.javasunderesan;

public class AddSpaceBeforeAndAfterString {
 public static void main(String[] args) {
  try {
   String msg = "java";
   System.out.println(String.format("%8s", msg)); // adding spaces
               // before
               // string ;
   System.out.println(String.format("%-8s", msg));// adding spaces
               // after
               // string
   // 8 is a number of characters String and spaces

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

Output::

    java
java    

No comments :

Post a Comment