Java 如何在字符串前添加空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31609082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:18:45  来源:igfitidea点击:

How to add whitespace before a string

javatext-align

提问by 99maas

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?

以上将在我手动完成的字符串之前给出 10 个空格,但是除了添加手动空格之外,还有其他方法可以指定字符串之前的空格吗?

采纳答案by CoderNeji

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 Stringand no of spaces needed....

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

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

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

回答by Gaurav Mahawar

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

回答by Zeeshan

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

回答by Lathy

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.

您可以更改修改 for 循环中的空间计数。

回答by cahen

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

Result:

结果:

|          Hello Word!!|

10 whitespaces added

添加了 10 个空格

回答by Bhanupratap Singh

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

回答by Bhanupratap Singh

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

    }
}

回答by Juan Gierbolini

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.

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

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

}

}