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,665
  • 4
  • 70
  • 110
Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133

3 Answers3

1

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

Pedantic
  • 5,032
  • 2
  • 24
  • 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,271
  • 3
  • 13
  • 34
0

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

String delimiter = "\\n";
Timothy Lee Russell
  • 3,719
  • 1
  • 35
  • 43
  • 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