在Java中将格式化字符串转换为HTML(Converting formatted string into HTML in Java)
我发布一个带空格的消息(使用string.format计算)并尝试以HTML格式显示。 但没有这样做。
码:
final String format = "|%1$-35s|%2$-20s|%3$-30s|%4$-20s|\n";
str = String.format(format,currentInputTag,currentInputVal,currentOutTag,currentOutVal)
str.replaceAll(" ", " ");
字符串输出(我希望它的样子):
|Input tag |Value |Output tag |Value |
|FlightLegReportedUtcDatetime |2012-07-25T01:54:00 |ns3:EstOffGroundDateTime |2012-07-25T01:54:00 |
但我得到:
HTML:
label |输入标签|值|输出标签|值| /标签
label | FlightLegReportedUtcDatetime | 2012-07-25T01:54:00 | ns3:EstOffGroundDateTime | 2012-07-25T01:54:00 | / label
我不确定,即使空格的数量是正确的,我也看到如上所示的输出。 BTW删除<>以显示标签。
I am publishing a message with spaces (calculated using string.format) and trying to show in HTML as it is. But failing to do so.
Code:
final String format = "|%1$-35s|%2$-20s|%3$-30s|%4$-20s|\n";
str = String.format(format,currentInputTag,currentInputVal,currentOutTag,currentOutVal)
str.replaceAll(" ", " ");
String output(the way I want it to look like):
|Input tag |Value |Output tag |Value |
|FlightLegReportedUtcDatetime |2012-07-25T01:54:00 |ns3:EstOffGroundDateTime |2012-07-25T01:54:00 |
But I get:
HTML:
label|Input tag |Value |Output tag |Value |/label
label|FlightLegReportedUtcDatetime |2012-07-25T01:54:00 |ns3:EstOffGroundDateTime |2012-07-25T01:54:00 |/label
I am not sure, even though the number of spaces are correct I see the output as shown above. BTW removing <> to show the tags.
原文:https://stackoverflow.com/questions/11726030
更新时间:2019-10-31 16:10
最满意答案
您可以编辑代码,将输出包装到
这将带来有效的html和(如果没有定义不同)固定宽度字体。You can edit your code an wrap the output into
this will bring valid html and (if not defined different) a fixed width font.2012-07-30
相关问答
String s = String.format("x:%d, y:%d, z:%d", x, y, z);
Java Howto - 格式化字符串 String s = String.format("x:%d, y:%d, z:%d", x, y, z);
Java Howto - Format a string
我最终在创建并删除它的跨区文本末尾搜索空格。 由于
SpannableStringBuilder spannableString) {
if (spannableString == null)
return new SpannableStringBuilder("");
...
有一个CodeRay着色器。 CodeRay.scan("5.times do\n puts 'Hello, world!'\nend", :ruby).
div(:line_numbers => :table)
我用它来突出显示我的wiki中的JSON片段,所以如果你把对象打印到JSON然后通过coderay传递它,它肯定会起作用。 There's a CodeRay colorizer. CodeRay.scan("5.times do\n puts 'Hello, wo
...
以下是将文本转换为HTML的一些代码,但您确实需要更好地解释“格式化”的含义。 例如,你说你的输入没有换行符? 那么应该如何格式化呢? 你想知道在哪里插入换行符,然后将输入分成段落吗? Here is some code to convert text to HTML but you'd really need to better explain what you mean by "formatted". For example, you said your input has no newlin
...
这就像你所追求的: 编码和显示HTML - DotNetSlackers Is this something like what you are after: Encode and Display HTML - DotNetSlackers
String.format是你要找的它返回一个String而不是像System.out.format这样的PrintStream 。 你的代码应该是: WRITE_MY_LINE(String.format(" I = %3d var = %9.6f", i, myVar));
查看Java.lang.String.format()方法以获取更多信息。 String.format is what you are looking for it returns a String and not a
...
你的代码像这样结束: [...]