Prabha Vathi
Prabha Vathi

Reputation: 347

newline - not added or unable to split by newline

I am reading android. So, It is just for practice.

String storedData = readData();
String lines[] = storedData.split("\\n");

int linesLength = lines.length;
        for(int i = 0; i < linesLength; i++) {
            tmp = "it is me " + tmp + lines[i] + "\r\n";//i have assigned all the variables.
        }
textbox.setText(tmp);

Everything is displayed in single line.

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

Tried all the above. Not working for me.

This is how i have written the text in a file.
Am i writing the new content in the storedData with newline? Is that correct? I assume storedData has a newline in the end. So i join the content and add a newline in the end.

content = storedData + content + "\n";
outputStream = openFileOutput(FILENAME, Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.close();

it is always displaying the output in single line.

textbox.setText(tmp);
it is me test test1 test2

in the below line,

content = storedData + content + "\n";
//content = a word received from EditText
//storedData = content read from a stored file. 

Upvotes: 2

Views: 220

Answers (1)

Pradip
Pradip

Reputation: 3177

replace this:

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

with

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

Actually your lines[] is containing only one line no splits occurs. try that.

Upvotes: 1

Related Questions