[英]Replace spaces with '-' with string.format?
I'm printing out a list of things and I formatted the text with spaces. 我正在打印事物列表,并用空格格式化了文本。 but instead of spaces I just want all dashes.... I cant figure out or find a way to do this and at the same time keep the text formatted correctly. 但是我只希望所有的破折号而不是空格。...我无法弄清楚或找到一种方法来做到这一点,同时保持文本的格式正确。
here is my format code: 这是我的格式代码:
System.out.println(String.format("Word:%11s-----------Hash Value:%5s ",arrayString[i],arrayNums[i]));
here is an example output: 这是示例输出:
Word: DO-----------Hash Value: 2
Word: END-----------Hash Value: 3
Word: ELSE-----------Hash Value: 4
Word: CASE-----------Hash Value: 5
Word: DOWNTO-----------Hash Value: 6
Word: GOTO-----------Hash Value: 7
Word: TO-----------Hash Value: 8
Word: OTHERWISE-----------Hash Value: 9
Word: TYPE-----------Hash Value: 10
Word: WHILE-----------Hash Value: 11
Word: CONST-----------Hash Value: 12
Desired output: 所需的输出:
Word:---------DO-----------Hash Value:----2
This will work. 这将起作用。
String stringToPrint = String.format("Word:%11s-----------Hash Value:%5s ",arrayString[i],arrayNums[i]);
stringToPrint = stringToPrint.replace(' ', '-');
System.out.println(stringToPrint);
You could do 你可以做
System.out.println(String.format("Word:%11s-----------Hash Value:%5s ".replace(" ", "ƒ"),
arrayString[i], arrayNums[i]).replace(" ", "-").replace("ƒ", " "));
Something like this? 像这样吗
public class Dash
{
public static void main (String[] args)
{
String dashes = "-------------";
String[] verbs =
{ "DO", "END", "ELSE", "CASE", "DOWNTO", "GOTO", "TO", "OTHERWISE", "TYPE",
"WHILE", "CONST" };
for (String verb : verbs)
System.out.printf ("%s%s%s%n", dashes.substring (verb.length ()), verb, dashes);
}
}
which gives 这使
-----------DO-------------
----------END-------------
---------ELSE-------------
---------CASE-------------
-------DOWNTO-------------
---------GOTO-------------
-----------TO-------------
----OTHERWISE-------------
---------TYPE-------------
--------WHILE-------------
--------CONST-------------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.