Tag Archives: Line Break

Line Break in Apex PageMessage

You can add \n for HTML NewLine character in Apex String, and you can replace each \n by <br/> in apex:pageMessage using JSENCODE function.

Apex Controller:

public class SFController{  
    public String msg {get;set;}
    public SFController(){  
        msg = 'Error Message 1' + '\n' + 'Error Message 2' + '\n' + 'Error Message 3';  
    }  
}

Visualforce Page:

<apex:page controller="SFController">
    <apex:pageMessage summary="{!SUBSTITUTE(JSENCODE(msg), '\\n', '<br/>')}" severity="Error" strength="3" escape="false" />
</apex:page>

Output:

Show Apex String With NewLine in Visualforce Page

You can add \n for HTML NewLine character in Apex String, and you can replace each \n by <br/> in Visualforce page using JSENCODE function.

SUBSTITUTE(JSENCODE(Variable), '\\n', '<br/>')

Apex Controller:

public class SFController{  
    public String myVariable {get;set;}  
    public SFController(){  
        myVariable = 'Sales Cloud' + '\n' + 'Service Cloud' + '\n' + 'Community Cloud';  
    }  
}

Visualforce Page:

<apex:page controller="SFController">   
    <apex:outputLabel value="{!SUBSTITUTE(JSENCODE(myVariable), '\\n', '<br/>')}" escape="false"/ >   
</apex:page>  

Output: