8

I need your help in adding whitespace before a string as I need to format the String value to be in a specific position in the page. For example:

System.out.println("          Hello Word!!");  

The above will give 10 spaces before the String which I did them manual, but is there any other way to specify the space before the String other than adding manual spaces?

99maas
  • 1,239
  • 12
  • 34
  • 59

8 Answers8

8
String newStr = String.format("%10s", str);
Gaurav Mahawar
  • 412
  • 3
  • 5
  • 14
  • 2
    this is wrong, it won't add 10 whitespaces, it will be 10 minus the string length. Also, the "-" will put the whitepaces in the end – cahen Jul 24 '15 at 11:18
  • 1
    I need to keep spaces at the beginning of the String – 99maas Jul 24 '15 at 11:29
8

Consider this as your code....

    public static void main(String[] args) {

        String hello = "hello";
        Brute b = new Brute();
       System.out.println( b.addspace(1,hello));
    }

    String addspace(int i, String str)
    {       
        StringBuilder str1 = new StringBuilder();
            for(int j=0;j<i;j++)
            {
                str1.append(" ");
            }
            str1.append(str);           
            return str1.toString();         

    }

This will add desired no of spaces in the string at its beginning...

Just pass your input String and no of spaces needed....

As addspace(<no_of_spaces>,<input_string>);

CoderNeji
  • 2,056
  • 3
  • 20
  • 31
  • 3
    other solutions will do all that in 1 or 2 line, I don't agree this should be the accepted solution – cahen Jul 24 '15 at 11:48
4
String str = "Hello Word!!";
String.format("%1$" + (10 + str.length()) + "s", str);

Result:

|          Hello Word!!|

10 whitespaces added

Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98
cahen
  • 15,807
  • 13
  • 47
  • 78
1

You can write your own fumction:

public static void main(String[] args) {
        String myString = "Hello Word!!";
        System.out.println(getWhiteSpace(10)+myString);
    }

    private static String getWhiteSpace(int size) {
        StringBuilder builder = new StringBuilder(size);
        for(int i = 0; i <size ; i++) {
            builder.append(' ');
        }
        return builder.toString();
    }
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
1

This may be useful to you,

    String s = "%s Hellow World!";
    StringBuilder builder = new StringBuilder();
    for(int i=0;i<10;i++){
        builder.append(" ");
    }

    System.out.println(s.format(s,builder.toString()));

You can change the modify the count of space in the for loop.

Lathy
  • 879
  • 1
  • 8
  • 25
1
import java.io.*;
import java.util.*;
class spaceBeforeString
{
    public static void main(String args[])
    {
        String str="Hello";    

        for(int i=0;i<10;i++)
        {
                str=" "+str;
        }
        System.out.println(str);
    }
}
1
import java.util.*;
import java.io.*;

class AddSpaceDemo
{
    String str;
    int noOfSpaces;
    Scanner sc=new Scanner(System.in);

    void getInput()
    {

        System.out.println("Enter the string before which the space is to be added: ");
        str=sc.next();

        System.out.println("Enter the no. of spaces to be added before the string: ");
        noOfSpaces=sc.nextInt();

    }

    String addSpaceBefore()
    {
        for(int i=0;i<noOfSpaces;i++)
        {
            str=" "+str;
        }
        return str;
    }




}



class AddSpace
{

    public static void main(String args[])
    {
        String s;
        AddSpaceDemo a=new AddSpaceDemo();
        a.getInput();
        s=a.addSpaceBefore();
        System.out.println("String after adding whitespace before string: ");
        System.out.println(s);

    }
}
0

I'm making a basic Java POS System. You set how many characters fits on the paper width and it align both to the left and to the right.

public class Main {

int width = 32;


public static void main(String[] args) {


    String dash = "--------------------------------";
    String item = "COMPANY NAME";
    String price = "00.00";
    String string = alignment(item, price);


    String description = "123-456-7890";
    String tax = "0.00";
    String string2 = alignment(description, tax);


    System.out.println(dash);
    System.out.println(string);
    System.out.println(string2);

}


private static String alignment(String item, String price) {

    String s = "";

    s += item;

    int x = 0;
    while(x < width - item.length() - price.length()) {
        s += " ";
        x++;
    }

    s += price;

    return s;
}

}