用String.format()打印空格 [英] print spaces with String.format()
问题描述
pre $
for(int i = 0; i< numberOfSpaces; i ++){
System.out.print();
使用 我很确定这是可能的,但 javadoc有点混乱 您需要指定字段的最小宽度。 为什么要生成一定长度的空格字符串? 如果你想要一个长度为这个值的列,那么你可以这样做: 这给了你numberOfSpaces-5空格后跟Hello。如果您希望Hello出现在左侧,请在numberOfSpaces之前添加一个减号。 how I can rewrite this: using PS I'm pretty sure that this is possible but the javadoc is a bit confusing. You need to specify the minimum width of the field. Why do you want to generate a String of spaces of a certain length. If you want a column of this length with values then you can do: which gives you numberOfSpaces-5 spaces followed by Hello. If you want Hello to appear on the left then add a minus sign in before numberOfSpaces. 这篇关于用String.format()打印空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! String.format()$ c
$ b
String.format(%+ numberOfSpaces +s,);
$ b
String。格式(%+ numberOfSpaces +s,Hello);
for (int i = 0; i < numberOfSpaces; i++) {
System.out.print(" ");
}
String.format()
?String.format("%" + numberOfSpaces + "s", "");
String.format("%" + numberOfSpaces + "s", "Hello");