I need to break the paragraph at the end of the line and add it in next line. So i used substituting new line(\n) with break tag(

Here is the tag that i'm using on vf page.

   <apex:outputText value="{!SUBSTITUTE(JSENCODE(quote.Description),'\n','<br/>')}" style="white-space:pre;" escape="false" />

it works fine but now i have an extra "\r" on each line. is there anyway i can get rid of this '\r' tag? enter image description here

  • Is there a reason as to why you cannot use apex:outputField instead of using apex:outputText ? Jul 13, 2018 at 22:41
  • <apex:outputField doesnt allow attribute escape="false". that is why using outputText. Jul 13, 2018 at 22:45

1 Answer 1


There's actually a variety of line breaks used in the world, the common types today being LF (\n) and CRLF (\r\n). The reason for this is historical, but it's important to know that if you're using Windows, you're in the CRLF group, and if you're using Linux, you're in the LF group, and some other platforms might even be using just CR (but I don't know of any modern OSes that do this).

To fix the problem, you would need to take an extra step to remove the \r characters as well. However, I would say that all of this simply isn't necessary. Simply use the CSS white-space: pre-line and don't substitute line breaks; they should not be necessary. Also, using escape="false" is inherently risky, so I would advise against this.

Here's an example of preserving line spaces in the output:

Sample Controller

public class q225036 {
    public String getOutput() {
        return 'Hello,\r\nWorld!\r\n\r\n\r\nGoodbye,\r\nWorld!';

Sample Page

<apex:page controller="q225036">
    <apex:outputText style="white-space: pre-line" value="{!output}" />

The output:



You don't need to use SUBSTITUTE, and you don't need to use JSENCODE. This also prevents people from injecting malicious scripts and so on in to your page (which, by the way, JSENCODE wouldn't protect you from, you'd need to use HTMLENCODE instead). This technique also works even if you don't use a controller (I'm using one just to keep it self-contained).


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .