3

my file contains this string:

a b c 

now I want to read it and split it with empty line so I have this:

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

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

1
  • Can you post an output of what you need? Or are you just trying to copy the input to the output? Commented Jul 30, 2012 at 8:20

8 Answers 8

7
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 by empty line, try \n\r\n\r. Or you can use .readLine() to read your file, and skip all empty lines.

Are you sure it's 10 13 10 13? It always should be 13 10...

And, you should not depend on line.separator too much. Because if you are processing some files from *nix platform, it's \n, vice versa. And even on Windows, some editors use \n as the new line character. So I suggest you to use some high level methods or use string.replaceAll("\r\n", "\n") to normalize your input.

Sign up to request clarification or add additional context in comments.

5 Comments

I think he left out the leading 0x13. Windows line endings are CARRIAGE RETURN followed by LINE FEED. (\r\n). Unless somebody is playing a joke on the guy and the lines are separated by \n\r like on the Acorn BBC (en.wikipedia.org/wiki/BBC_Micro)
and how I can get '\r\n' independeet from os ? System.getProperty("line.separator") gives me just \n
@hudi You should not depend on line.separator too much. Because if you process some file from *nix, it's \n, vice versa. And even on Windows, some editors use \n as the new line character.
you are rigth it is 13 10 my mistake
I had to do this in js. \n\r\n\r did the trick for me when nothing else did.
2

Keep in mind, sometimes you have to use:

System.getProperty("line.separator"); 

to get the line separator, if you want to make it platform independent. You can also use BufferedWriter's newLine() method, that takes care of that automatically.

2 Comments

when I convert System.getProperty("line.separator"); to byte is return just 13 so I cant split it by NEWLINE + NEWLINE
there's difference in what that method returns, depending on the OS - that's the whole point... Check out docs.oracle.com/javase/tutorial/essential/environment/…
1

Try using:

text.split("\n\r"); 

Comments

0

Why are you splitting on \n\n?

You should be splitting on \r\n because that's what the file lines are separated by.

1 Comment

hm ok and how I can split this file indipendent from os ? System.getProperty("line.separator") gives me just \n
0

Try to use regular expressions, something like:

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

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

1 Comment

"a b c\r\n\r\nd".split("\\s+") returns ["a", "b", "c", "d"], it does not fit OP's requirement.
0
LF: Line Feed, U+000A CR: Carriage Return, U+000D so you need to try to use "string".split("\r\n"); 

Comments

0

Use scanner object, instead of worrying about chars/bytes.

Comments

-1

One Solution is to Split using "\n" and neglect empty Strings

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

1 Comment

It's telling me split returns String[]. And why do you compare strings using !=?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.