6

My controller code is as follows -

       for(String resStr : responseArray){
            if(resStr.indexOf('r_error_str') != -1){
                responseString += resStr.substring(resStr.indexOf('=') + 1); 
                responseString += '\n';
            }
        }

but when i display this 'responseString ' String using 'apex:outputLabel or apex:outputText or as Exception message '\n' is not converting in new line. Whole String is coming in one line.

Can anyone explain why is this happining

5 Answers 5

10

just use <br/> in controller and escape="false" in visualforce text.

3
  • 5
    This does work of course, but if you're looking to publish on the AppExchange the use of escape="false" will be heavily frowned upon (from experience).
    – Matt Lacey
    Dec 23, 2012 at 0:26
  • It is not working if i set message in user define exception object as message and rending that at visual force page. It is showing <br/> at visual force error message. Dec 23, 2012 at 15:13
  • It works as expected.
    – Cray Kao
    Aug 7, 2019 at 14:16
8

The simple answer is using '\n' will not work in HTML elements as it is just basically white space, google your problem and you'll get lots of results.

Could you possibly 'repeat' in your VF on the responseArray?

7
  currentAddress = currentStreet;
        currentAddress += '\r\n';
        currentAddress += currentCity + + ' ' + currentState  + ' ' + currentZIP ;
        currentAddress += '\r\n';
        currentAddress += currentCountry;

using the '\r\n' works with outputtext .Please try same

1
  • 1
    Hey this worked for LWC's JS as well! Jul 22, 2020 at 12:59
3

I was trying the line breaks (\r\n) in a trigger, and couldn't get them to work, so I tried adding a non-breaking space character, and that worked like a charm.

   for(String resStr : responseArray){
        if(resStr.indexOf('r_error_str') != -1){
            responseString += resStr.substring(resStr.indexOf('=') + 1); 
            responseString += '&nbsp;';
        }
    }
0

In Apex class to break the line you need to use below code.enter image description here

1
  • 1
    This is the same as an existing answer from 10 years ago, but with less information.
    – Derek F
    Jan 23, 2023 at 12:15

This site is temporarily in read-only mode and not accepting new answers.

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