如何在字符串前添加空格

How to add whitespace before a string

我需要你的帮助在字符串前添加 whitespace,因为我需要将字符串值格式化为页面中的特定位置。例如:

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

以上将在我手动完成的字符串之前给出 10 spaces,但是除了添加手动 [=15= 之外,还有其他方法可以在字符串之前指定 space ]s?

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

将此视为您的代码....

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

    }

这将在字符串的开头添加所需的空格数...

只需传递您的输入 String 并且不需要空格....

作为addspace(<no_of_spaces>,<input_string>);

您可以编写自己的函数:

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

这可能对你有用,

    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.

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

结果:

|          Hello Word!!|

添加了 10 个空格

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

    }
}

我正在制作一个基本的 Java POS 系统。您可以设置适合纸张宽度的字符数,并同时向左和向右对齐。

public class 主要{

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

}