java 如何将 \n 放入属性文件的值中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11214681/
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
How do I put a \n into a property file's value?
提问by iank
I'm using Java Property files and would like to have a newline character put into a value (used as the contents of a message to the user), so that this output contains a new line.
我正在使用 Java 属性文件,并希望将换行符放入一个值中(用作发送给用户的消息的内容),以便此输出包含一个新行。
I tried putting it in as a \\n (as I have to escape the backslash character), but I just end up with a message with \n written in it.
我尝试将它作为 \\n 放入(因为我必须转义反斜杠字符),但我最终得到了一条写有 \n 的消息。
ie.
IE。
message=Dear Foo,\n\nThank you for signing up for Bar.
gives:
给出:
Dear Foo,\n\nThank you for signing up for Bar.
I want:
我想:
Dear Foo,
Thank you for signing up for Bar.
How can I fix this? Thanks!
我怎样才能解决这个问题?谢谢!
回答by Nitin Chhajer
Simply use \nfor this purpose
仅用\n于此目的
You are escaping the escape character using \\hence you are getting \nprinted
您正在使用转义符转义,\\因此您正在\n打印
回答by GETah
This should do it:
这应该这样做:
message=Dear Foo,\nThank you for signing up for Bar.

