75

I'm creating an android application and within it there is a button that will send some information in an email, and I don't want to have everything all in one paragraph.

Here's what my app is doing for the putExtra for the email's body:

I am the first part of the info being emailed. I am the second part. I am the third part.

Here's what I want it to do:

I am the first part of the info being emailed.
I am the second part.

I am the third part.

How would I put a new line into a string or with the putExtra method to accomplish that?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Reed
  • 14,703
  • 8
  • 66
  • 110

5 Answers5

105

Try:

String str = "my string \n my other string";

When printed you will get:

my string
my other string

Thomas Vos
  • 12,271
  • 5
  • 33
  • 71
user824013
  • 1,051
  • 1
  • 7
  • 2
  • 1
    This is not system independent. Some systems use \r\n for line breaks – Gibolt Mar 10 '18 at 22:34
  • 1
    @Gibolt This IS system independent. Android runs only on Linux based devices and therefore the \n is consistent among all devices. – Johann Mar 02 '21 at 14:13
91

Try using System.getProperty("line.separator") to get a new line.

keno
  • 2,956
  • 26
  • 39
  • 16
    Also, I learned putting \n within a string inserts a new line – Reed Aug 09 '11 at 23:33
  • 3
    @Jakar `System.getProperty("line.separator")` is platform independent and `\n` is not. But if you're developing android apps the platform is fixed and linux/android uses `\n` as newline char. [wikipedia/Newline](https://en.wikipedia.org/wiki/Newline) – wischi Mar 18 '15 at 17:01
  • 2
    @wischi, Good point. And `\r\n` is more cross-system-compatible I believe (correct me if I'm wrong) and is better for transferring data as a result. Such as if you send an email using `\n` to and it's read on a system that uses `\r\n`, you may lose lines, whereas in most cases if you pass `\r\n` to a system wanting just `\n`, it will compensate. I might be wrong here, and I don't have a source. This is just what I've gathered over time. – Reed Mar 18 '15 at 17:23
27

I would personally prefer using "\n". This just puts a line break in Linux or Android.

For example,

String str = "I am the first part of the info being emailed.\nI am the second part.\n\nI am the third part.";

Output

I am the first part of the info being emailed.
I am the second part.

I am the third part.

A more generalized way would be to use,

System.getProperty("line.separator")

For example,

String str = "I am the first part of the info being emailed." + System.getProperty("line.separator") + "I am the second part." + System.getProperty("line.separator") + System.getProperty("line.separator") + "I am the third part.";

brings the same output as above. Here, the static getProperty() method of the System class can be used to get the "line.seperator" for the particular OS.

But this is not necessary at all, as the OS here is fixed, that is, Android. So, calling a method every time is a heavy and unnecessary operation.

Moreover, this also increases your code length and makes it look kind of messy. A "\n" is sweet and simple.

Community
  • 1
  • 1
Aritra Roy
  • 15,355
  • 10
  • 73
  • 107
10

I use <br> in a CDATA tag. As an example, my strings.xml file contains an item like this:

<item><![CDATA[<b>My name is John</b><br>Nice to meet you]]></item>

and prints

My name is John
Nice to meet you
marcolav
  • 405
  • 1
  • 6
  • 17
0

If you want to add line break at runtime into a String from same string you are deriving the value then this Kotlin code works for me:

str = "<br>"+str?.replace("," , "</br><br>")+"</br>"
value = HtmlCompat.fromHtml(${skill_et_1}",Html.FROM_HTML_MODE_LEGACY)
tv.text = value
Mohit Kumar
  • 41
  • 1
  • 7