Thursday, April 25, 2013

[android help] How to replace new line characters with actual new lines in a string in android?

How to replace new line characters with actual new lines in a string in android? - Stack Overflow







Tell me more ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

















I need to display a string which contains a lot of new line '\n'.These needs to be replaced with actual newlines in that string. How can I do that in android?
































you should concatenate the newline operator like:



String str = "This is testing" + "\n" + "How are you?";


you said from database you could split like:



String[] separated = str.split("\n");
separated[0]; // this will contain "This is testing"
separated[1]; // this will contain "How are you?"


and then concatenate them

























What seems to be happening is the string you receive from your database contains not the new line character, but rather the characters \ and n. The simplest fix is use simply the String.replace function to replace the character sequence \ n by the character \n like so:



String str = getStringWithFakeNewlines();
str.replace("\\n", "\n"); // The first backslash is doubled to find actual backslashes



















default






.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...