gpt4 book ai didi

java - 如何在Java Formatter中转义减号?

转载 作者:行者123 更新时间:2023-11-30 02:07:37 24 4
gpt4 key购买 nike

这是格式化的表头示例:

StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb, Locale.US);
formatter.format("%-16s ---%-21s--- %-21s %-23s %-25s", "IP", "BLOCKED", "BLOCKED TILL", "ACTIVITY (00:00-24:00)", "COUNTRY");
System.out.println(sb.toString());

但问题出在---%-21s---部分:

IP               ---BLOCKED              --- BLOCKED TILL          ACTIVITY (00:00-24:00)  COUNTRY

如何转义最后三个减号?我想强调排序列。结果应该是这样的:

IP               ---BLOCKED---               BLOCKED TILL          ACTIVITY (00:00-24:00)  COUNTRY

最佳答案

问题是您指定了 BLOCKED 字符串的宽度为 21,因此破折号 --- 无法立即包围文本(请注意第二组破折号位于一堆空格之后)。相反,您必须将破折号放在要打印的字符串内:

formatter.format("%-16s %-21s %-21s %-23s %-25s", "IP", "---BLOCKED---", "BLOCKED TILL", "ACTIVITY (00:00-24:00)", "COUNTRY");

关于java - 如何在Java Formatter中转义减号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50974550/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com