How to add a space before a line

I need your help adding spaces before the string as I need to format the String value at a specific position on the page. For example:

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

      

The above will have 10 spaces before the line, which I made them manually, but is there any other way to put a space before the line other than adding manual spaces?

+3


source to share


7 replies


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 the desired amount of spaces in the line at the beginning ...



Just pass your input String

and no spaces needed ....

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

+2


source


String newStr = String.format("%10s", str);

      



+3


source


String str = "Hello Word!!";
String.format("%1$" + (10 + str.length()) + "s", str);

      

Result:

|          Hello Word!!|

      

added 10 added spaces

+1


source


You can write your own nonsense:

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();
    }

      

0


source


This might be helpful for 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 change in the number of spaces in the for loop.

0


source


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);
    }
}

      

0


source


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


source







All Articles