How to split a line with an empty new line

my file contains this line:

a b c 

now I want to read it and split it into an empty string so that I have this:

 text.split("\n\n"); where text is output of file 
Problem

is that this does not work. When I convert the new line to byte, I see that "\ n \ n" is represented as 10 10, but the new line in my file is represented as 10 13 10 13. So, how can I split my file?

+3
source share
8 answers
 Escape Description ASCII-Value \n New Line Feed (LF) 10 \r Carriage Return (CR) 13 

So you need to try string.split("\n\r") in your case.

Edit

If you want to split an empty string, try \n\r\n\r . Or you can use .readLine() to read your file and skip all empty lines.

Are you sure this is 10 13 10 13 ? It should always be 13 10 ...

And you should not depend heavily on line.separator . Because if you are processing some files from the * nix platform, it \n is the other way around. And even on Windows, some editors use \n as the newline character. Therefore, I suggest you use some high-level methods or use string.replaceAll("\r\n", "\n") to normalize the input.

+6
source

Try using:

 text.split("\n\r"); 
+2
source

Keep in mind sometimes you need to use:

 System.getProperty("line.separator"); 

to get a line separator if you want to make it platform independent. You can also use the BufferedWriter newLine () method, which will take care of this automatically.

+2
source

Why are you cracking \n\n ?

You must divide by \r\n because it is that the lines of the file are split.

0
source

One solution is to split using "\ n" and ignore blank lines

 List<String> lines = text.split("\n"); for(String line : lines) { line = line.trim(); if(line != "") { System.out.println(line); } } 
0
source

Try using regular expressions, for example:

 text.split("\\W+"); 

Strike>

 text.split("\\s+"); 
0
source
 LF: Line Feed, U+000A CR: Carriage Return, U+000D so you need to try to use "string".split("\r\n"); 
0
source

Use a scanner object, not worry about characters / bytes.

0
source

Source: https://habr.com/ru/post/912719/


All Articles