在 java String.format 中转义格式化字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/201312/
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
escaping formatting characters in java String.format
提问by Omar Kooheji
This question is pretty much the same as this .Net questionexept for java.
这个问题与这个 .Net 问题除java 之外的问题几乎相同。
How do you escape the %1$characters in a java String.format?
你如何转义%1$java 中的字符String.format?
THe reason I need to do this is that I'm building up a string that will later have more info inserted into it. I've thought of having one of the args just be "%1$"but that doesn't seem to be very elegant?
我需要这样做的原因是我正在构建一个字符串,稍后将在其中插入更多信息。我曾想过让其中一个 args 存在,"%1$"但这似乎不是很优雅?
sorry if this is obvious my java is a tad rusty.
抱歉,如果这很明显,我的 Java 有点生疏了。
回答by Draemon
You can just double up the %
你可以加倍%
回答by gizmo
Either you can use the proposal of Draemon, either you can also have a look at java.text.MessageFormat classthat has more powerfull formatting abilities than String.format()
要么你可以使用Draemon的提议,要么你也可以看看java.text.MessageFormat类,它具有比String.format()更强大的格式化能力

