从mysql读取数据时如何拆分java中的“\ n”? [英] How to split a “\n” in java when read the data from mysql?
问题描述
在我的Java代码中,我定义了一个字符串`String strExample ='abc \ ndef'`。现在我想分开它。所以我试着得到字符串的长度。
int strArrlength = strExample.split( \ n)。length
结果是2.
但是当我尝试将strExample写入mysql表,然后从表中读取它时,在相同的代码中我得到的长度为1.
我的开发环境是Windows 8.1,数据库安装在CentOS 6.5中。
怎么了?我试图更改代码如下:
strExample.split( \\ n),split( \\\\ n ),split( \ n +),split( \\\\?\\ n)
但是所有这些都返回1;
这是我的测试,我尝试从mysql中选择Hex字符串。但仍然不能正常工作。
< pre lang =java> private final static 字符串 mHexStr1 = 5468697320697320612074657374EFBC815C6E204F6E6C792061207465737421 ;
public static void main ( String [] args){
String testStr = hexStr2Str(mHexStr1);
System.out.print(testStr + ; + testStr.split( \\ n)。length); // 返回1
字符串 abc = 这是一个测试!\ n只有一个测试!;
System.out.println(abc + ; + abc.split( \ n)。length); // return 2
}
目前尚不清楚你想要做什么,但你的逃脱是不正确的。换行符只需要一个反斜杠,例如:
字符串 strExample = \ nrain \\\
in \ nSpain;
string [] substrings = strExample.split( \ n);
int arraySize = substrings.length;
System.out.println( 数组计数: + arraySize);
for ( String foo:substrings)
{
System。 out.println( item: + foo);
}
In my Java code I define a string `String strExample='abc\ndef'`. Now I want to split it. So I try to get the length of the string.
int strArrlength=strExample.split("\n").length
The result is 2.
But when I try to write the strExample in to mysql table, and then read it from the table, in the same code I get the length is 1.
My development environment is Windows 8.1, and the database is installed in CentOS 6.5.
What's wrong? I've tried to change the code like:
strExample.split("\\n"),split("\r\n"),split("\n+"),split("\\r?\\n")
but all of them return 1;
Here is my test,I try to select Hex string from mysql.but still not work.
private final static String mHexStr1="5468697320697320612074657374EFBC815C6E204F6E6C792061207465737421";
public static void main(String[] args) {
String testStr=hexStr2Str(mHexStr1);
System.out.print(testStr+";"+testStr.split("\\n").length);//return 1
String abc="This is a test!\n Only a test!";
System.out.println(abc+";"+abc.split("\n").length);//return 2
}
It is not clear what you are trying to do, but your escapes are incorrect. You only need a single backslash for a newline character, like:
String strExample = "The\nrain\nin\nSpain"; string[] substrings = strExample.split("\n"); int arraySize = substrings.length; System.out.println("Array count: " + arraySize); for (String foo : substrings) { System.out.println("item: " + foo); }
这篇关于从mysql读取数据时如何拆分java中的“\ n”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!