Escaping spaces in a Properties file in Java
Writing by shivdev on Monday, 5 of December , 2011 at 6:25 pm
Problem 1:
Can you escape keys or values in a properties file? Yes. Use backslash \ as the escape character.
So Hello World=foo should be written as Hello\ World=foo
Wikipedia has an excellent article
Problem 2:
You might most probably use the String replaceAll(String regex, String replacement) method. So how do you escape spaces?
// This Will NOT WORK !!
String escaped = “Hello World”.replaceAll(” “, “\\ “);// This is the right way to do it, since we need to work with regex
String escaped = “Hello World”.replaceAll(“\\s”, “\\\\ “);
Leave a comment
Category: Java
- Add this post to
- Del.icio.us -
- Digg -
- -
- Tweet -
-
-