Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

string - Java is not treating " " as new line when retrieved from Database column

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 as a value in DB and getting it in java using rs.getString(). I am getting proper value .

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 " " is encountered.Code works fine if i declare the newline as

String newLine= "
";

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???

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If when you print the newLine you get 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...