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 '18 at 22:41
  • <apex:outputField doesnt allow attribute escape="false". that is why using outputText. Jul 13 '18 at 22:45

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).

  • +1 nice answer. Jul 14 '18 at 1:56
  • Thank you, that worked perfectly. appreciate you taking your time and helping out here. Jul 16 '18 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.