Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange String.split("\n") behavior

Tags:

java

split

I have a .txt text file, containing some lines.. I load the contain using the RequestBuilder object, and split the responseText with words = String.split("\n"); but i wonder, why the result is contains the "\n" part.. For example, my text:

abc
def
ghi

the result is,

words[0] = "abc\n"
words[1] = "def\n"
words[2] = "ghi\n"

Any help is highly appreciated. Thanks in advance.

like image 210
amateurs Avatar asked Dec 27 '10 15:12

amateurs


2 Answers

Try using string.split("\\n+"). Or even better - split("[\\r\\n]+")

like image 60
Bozho Avatar answered Nov 01 '22 15:11

Bozho


You may also want to consider String[] lines = text.split("\\\\n");

like image 13
tricknology Avatar answered Nov 01 '22 14:11

tricknology