function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JamesSSJamesSS 

\n isn't working in inside of string

How to use \n in inside of string? I have used.But its not working.Is any one to find the error in my code? My code Vf page : This is your new Page {!Testvariable } Controller: public with sharing class Test { public string Testvariable{get;set;} public Test (){ Testvariable = 'Male\n\nFemale'; } }
prakash_sfdcprakash_sfdc

Hi,

 

Try using <apex:outputText value="{!Testvariable}" escape="false"/>

 

Please mark it as solution and give Kudos if it helps.

JamesSSJamesSS
I am using label(Html Tag) instead of output text.Is it possible in label tag to get the answer?
prakash_sfdcprakash_sfdc

If you are using <label> then use <br/> instead of \n.

 

Please mark as answer and give kudos if it solves your issue.

JamesSSJamesSS
In that input have one error. 'Invalid character used in the string' I have used br tag .It indicates error.That's why i asked how to use \n in String?
JamesSSJamesSS
required= "

Please enter the Name" In that input have one error. "Invalid character used in the string" I have used
.It indicates error.That's why i asked how to use \n in String?
JamesSSJamesSS
i give the input tag, required= 'Please enter the Name' In that required , i have include break tag its working but it throws the below error. 'Invalid character used in the string' That's why i asked how to use \n in String?
sandeep@Salesforcesandeep@Salesforce

I think you can go with this way 

 

I checked and working fine

<apex:page controller="Test" >
<apex:outputlabel value="{!Testvariable}" escape="false" ></apex:outputlabel>
</apex:page>

 

public with sharing class Test
{
public string Testvariable{get;set;}
public Test (){
Testvariable = 'Male<br/><br/>Female';
}
}

 

Please mark this answer and give kudos if it help ful and let me know in case still issue.

JamesSSJamesSS
I am using label html tag.I am giving br tag inside in the string its working.But i gave \n its not working.Its print \n.that's y i asked you. for example required = "\nenter name" It prints '\nenter name'. In output text have the escape="false" option.Is any possible in Label tag?
sfdcfoxsfdcfox

Please keep in mind that "\n" will have no effect unless you are using a white-space CSS element (e.g. "pre", or any element with a style that preserves line breaks). That said, you do not need to "escape" the output at all; it's simply a matter of treating the white space with the respect it deserves:

 

<apex:page controller="page1">
<apex:outputLabel style="white-space: pre-line" value="{!testString}"/>
</apex:page>

 

public with sharing class page1 {
    public string testString { get { return 'Line 1\nLine 2'; } }
}