0

I need to split string with delimiter of \n when I use this code:

String delimiter = "\n";
String[] temp;
temp = description2[position].split(delimiter);
for (int i = 0; i < temp.length; i++) {
    holder.weeklyparty_text3.setSingleLine(false);
    holder.weeklyparty_text3.setText(temp[i]);
}

but not get split string from \n.

Adam Stelmaszczyk
  • 19,333
  • 4
  • 67
  • 107
Nikunj Patel
  • 21,388
  • 23
  • 87
  • 131

3 Answers3

1

You need to escape the backslash in the delimiter string: "\\n"

Pedantic
  • 5,012
  • 2
  • 23
  • 37
0

In order to support Unix and Windows new lines use:

String lines[] = String.split("\r?\n");

as described in:

Split Java String by New Line

Community
  • 1
  • 1
HsnVahedi
  • 1,031
  • 1
  • 11
  • 26
0

Split uses regex - so to split on a newline, you should use:

String delimiter = "\\n";
Timothy Lee Russell
  • 3,691
  • 1
  • 35
  • 42
  • Post the code that you are using to populate the description2 string array. If you are using a read line method, for instance, the \n might not be in your data. – Timothy Lee Russell Jan 26 '12 at 17:54