11

I am facing a weird problem.
What I am doing is I am storing some values in DB(oracle 11g) as varchar2 and fetching the values in java and working with the fetched data.
Now I have \n as a value in DB and getting it in java using rs.getString(). I am getting proper value \n.

String newLine=rs.getString("column_value");

Now i parse a HTML page and get the whole page as a string.Suppose the page has 3 lines each depiciting ssome informations like below:

Time: 08 AM - 11 AM
Duration : 36 minutes

Now in code i will pass "Duration :" to a method and that will return me "36 minutes".
What logic i use is i get the index of "Duration :" and read till "\n" is encountered.Code works fine if i declare the newline as

String newLine= "\n";

But if i get it from DB it does not work. I know i don't need to store this in DB but i don't want to hardcode these items. So i have to store it in DB only. Can anyone help me out in this???

Anubhab
  • 1,736
  • 2
  • 18
  • 28
  • could you be more clear on what u mean by "Now when i get a number of lines and want to break it using newline java is not interpreting as newline." – codeMan Mar 28 '13 at 09:58
  • Are you on Windows? Does `\r\n` work? – Keppil Mar 28 '13 at 09:59
  • `\n` is `\n`, regardless of where you get it from. If you have checked that the value in the variable is correct, the issue is in other place since Java does not magically know where the value of the variable comes from. – SJuan76 Mar 28 '13 at 10:00
  • 3
    Hello, is the '\n' stored in the DB as 2 characters, '\' and 'n'? – david Mar 28 '13 at 10:02
  • 1
    Note that the *string* `\n` stored in a database will be returned like `"\\n"` in Java. – JimmyB Mar 28 '13 at 10:03
  • @david: in DB it is stored as single string '\n' – Anubhab Mar 28 '13 at 10:05
  • @HannoBinder but when i try to print the value of the variable it prints '\n' only – Anubhab Mar 28 '13 at 10:07
  • Exactly the same as it does when you print `"\\n"`. Try for example to print `"\\n\n\\n"` to see what that means. - Another attempt: What does `newLine.length()` return? – JimmyB Mar 28 '13 at 10:13
  • @HannoBinder i just checked the length..it is printing `2`. – Anubhab Mar 28 '13 at 10:17
  • 1
    Right, and that's the problem: The string from the DB is *two* characters long, `'\'` and `'n'`, which is equivalent to the Java representation `"\\n"` which again is *not* the newline character that would be `'\n'`. – JimmyB Mar 28 '13 at 10:20
  • figured out the solution .. :) – Anubhab Mar 28 '13 at 10:22
  • 1
    Great :) - For reference see also: http://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters-in-java – JimmyB Mar 28 '13 at 10:23
  • Isnt there a way oracle can return it in such a way that java will interpret it correctly? – Anubhab Mar 28 '13 at 10:24
  • 1
    @Anubhab There has to be a newline chracter in the database instead of the two characters '\' 'n'. Then Java will see it as a newline character. – Paul Mar 28 '13 at 10:31

4 Answers4

12

If when you print the newLine you get \n in the output, you might have to unescape the string you get from the DB. The Apache Commons Lang have a method for that:

http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#unescapeJava(java.io.Writer, java.lang.String)

So you would have simply to call

 String newLine = StringEscapeUtils.unescapeJava(rs.getString("column_value"));

Hope this helps

david
  • 804
  • 7
  • 21
4

The problem is that the database is adding a escape character to your string, so you end up with \\n instead of \n. To fix this you could just replace \\n with \n.

String db_string = new String("This is a string from the db \\n This should be a new line, but is not."); 
db_string = db_string.replace("\\n", "\n");

If you print this you get:
This is a string from the db
This should be a new line, but is not. //well it is now..

Zelnoth
  • 164
  • 7
3

try this

String newline = System.getProperty("line.separator");
boolean hasNewline = word.contains(newline);
Ankit
  • 6,554
  • 6
  • 49
  • 71
  • 4
    Not a good idea. The `line.separator` property is determined by the *client's* system environment and may be different on different systems, while only one representation can be stored in the DB. Therefore, this may work on some clients and fail on others given the very same data from the DB. – JimmyB Mar 28 '13 at 10:11
0

If you are retrieving your data to a HTML page, you can do the following:

String YourString = YourString.replaceAll("\\n", "<br>");

If you are retrieving your data to something else, you can do this :

String YourString = YourString.replaceAll("\\\\n", "\n");

Note: \\\\n is for \\n with escape chars