从数据库列中检索时,Java 不会将“\n”视为新行

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

Java is not treating "\n" as new line when retrieved from Database column

javastringoracle11gescaping

提问by Anubhab

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

我正面临一个奇怪的问题。
我正在做的是将一些值存储在 DB(oracle 11g)中作为varchar2并在 java 中获取值并处理获取的数据。
现在我将\n作为 DB 中的一个值,并使用rs.getString(). 我得到了适当的价值\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:

现在我解析一个 HTML 页面并将整个页面作为一个字符串。假设该页面有 3 行,每行描述一些信息,如下所示:

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

现在在代码中,我将“持续时间:”传递给一个方法,这将返回“36 分钟”。
我使用的逻辑是我得到“持续时间:”的索引并阅读直到遇到“\n”。如果我将换行符声明为

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

但是如果我从 DB 得到它就行不通了。我知道我不需要将它存储在数据库中,但我不想对这些项目进行硬编码。所以我只能将它存储在数据库中。谁能帮我解决这个问题???

回答by david

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

如果您在打印时newLine获得\n了输出,则可能必须对从数据库中获得的字符串进行转义。Apache Commons Lang 有一个方法:

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

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

希望这可以帮助

回答by Ankit

try this

试试这个

String newline = System.getProperty("line.separator");
boolean hasNewline = word.contains(newline);

回答by Zelnoth

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.

问题是数据库正在向您的字符串添加转义字符,因此您最终使用的是 \\n 而不是 \n。要解决此问题,您只需将 \\n 替换为 \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..

如果你打印这个,你会得到:
这是来自数据库的字符串
这应该是一个新行,但不是。//现在是..

回答by Josiah Thobejane

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

如果要将数据检索到 HTML 页面,则可以执行以下操作:

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: \\\\nis for \\nwith escape chars

注意:\\\\n用于\\n转义字符