Java ResourceBundle 值中的前导空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10783523/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:33:11  来源:igfitidea点击:

Java ResourceBundle leading whitespace in values

javawhitespaceresourcebundle

提问by prongs

I'm trying out the example given at Oracle's documentations. The java file is hereas shown in that page. Now with this java file, I keep a LabelsBundle.propertiesfile in the same directory.

我正在尝试Oracle 文档中给出的示例。java文件在这里,如该页面所示。现在有了这个 java 文件,我LabelsBundle.properties在同一个目录中保存了一个文件。

# This is the default LabelsBundle.properties file
s1 = computer
s2 = disk
s3 = monitor
s4 = keyboard

it seems that the trailing whitespace is not ignored while reading the values and the leading whitespaces are ignored.

似乎在读取值时不会忽略尾随空格,并且会忽略前导空格。

So if I want leading spaces to be there, what do I do?

所以如果我想要引导空间在那里,我该怎么做?

In other words, what do I need to do to read the value of s1as

换句话说,我需要做什么才能读取s1as的值

 computer

instead of

代替

computer

?

?

回答by user268396

A Unicode escape sequence \u0020should do it, because the properties parser should recognise it as an actual value and the Java String should interpret \u0020as the Unicode code point for space.

Unicode 转义序列\u0020应该这样做,因为属性解析器应将其识别为实际值,而 Java 字符串应解释\u0020为空格的 Unicode 代码点。

Alternatively, IIRC, you can escape the space with a backslash like this \ my value. Works for keys, too.

或者,IIRC,您可以使用这样的反斜杠来转义空格\ my value。也适用于钥匙。

回答by Mikita Belahlazau

You can escape space with backlash:

您可以通过反冲来逃避空间:

s1 =\ computer