How to insert a new line in strings in Android

后端 未结 4 1477
生来不讨喜
生来不讨喜 2020-12-05 09:11

This may seem like a stupid question, but I\'ve been searching and can\'t find an answer.

I\'m creating an android application and within it there is a button that w

相关标签:
4条回答
  • 2020-12-05 09:44

    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
    
    0 讨论(0)
  • 2020-12-05 10:08

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

    0 讨论(0)
  • 2020-12-05 10:09

    Try:

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

    When printed you will get:

    my string
    my other string

    0 讨论(0)
  • 2020-12-05 10:10

    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.

    0 讨论(0)
提交回复
热议问题