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.
Try using
string.split("\\n+")
. Or even better -split("[\\r\\n]+")
Windows carriage returns (
"\r\n"
) shouldn't make a visible difference to your results, nor should you need to escape the regular expression you pass toString.split()
.Here's proof of both the above using
str.split("\n")
: http://ideone.com/4PnZiIf you do have Windows carriage returns though, you should (although strictly not necessary) use
str.split("\r\n")
: http://ideone.com/XcF3CIf split uses regex you should use "\\n" instead of "\n"
You may also want to consider
String[] lines = text.split("\\\\n");
Try using
string.split("\\\\n")
. It works for me.