Can the key in a Java property include a blank character?

独自空忆成欢 提交于 2019-11-28 20:08:25

As it seems the delimiter should be =, not space. Hence - keyValuePair.split("=") should do.

If you are loading this from a java .properties file, then you can extend java.util.Properties and override this method

public synchronized void load(InputStream inStream) throws IOException

so that it parses the properties correctly.

Veaceslav Serghienco

You can escape every thing in properties file with Java Unicode:

  • \u003d for =
  • \u0020 for whitespace

For example:

foo bar = barefoot

must be:

foo\u0020bar\u0020=\u0020barefoot

So will be:

key: "foo bar "
value: " barefoot"

Maybe you can escape the whitespaces: foo\ bar = barefoot

Edit: Oops, I did not see that you can't change the properties.

sleske

I assume by "properties", you mean a Java property file (as written/read by java.util.Properties).

Then, as you write yourself,

foo bar = barefoot

must indeed be interpreted as

key: foo
value: bar = barefoot

There's no way to configure this using the built-in Properties class. You must either manipulate your input (escape the whitespace, change it to _ and back...), or write your own parser. Writing your own parser is probably better, as obviously your input isn't really a Java properties file to begin with :-).

keyValuePair = keyValuePair.substring(0,indexOf("=")).replaceAll("\\s+") + 
               keyValuePair.substring(indexOf("="));  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!