Nikunj Patel
Nikunj Patel

Reputation: 22076

SPLIT with \n Delimiter

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.

Upvotes: 0

Views: 4971

Answers (3)

HsnVahedi
HsnVahedi

Reputation: 1366

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

Upvotes: 0

Timothy Lee Russell
Timothy Lee Russell

Reputation: 3748

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

String delimiter = "\\n";

Upvotes: 0

Pedantic
Pedantic

Reputation: 5022

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

Upvotes: 1

Related Questions