1

I am trying to display US State inside single quote in Visualforce page. Please find below the visualforce page code where tax rate is param {0} and US State name is param {1}.

<apex:outputText value="{!IF(Tax__c.Rate=='0','No state tax applied','Tax rate is {0} percent for state {1}')}">
   <apex:param value="{!Tax__c.Rate}"/>
   <apex:param value="{!Tax__c.StateName}"/>                       
</apex:outputText>

I tried using \' before param {1}--that is 'Tax rate is {0} percent for state \'{1}'\'. But it is not working and displaying \ instead of '. Do you have any solution to display single quote or double quote in apex:outputText in Visualforce page?

1

I would personally put the quotes in the parameter, instead:

<apex:outputText value="{!IF(Tax__c.Rate=='0','No state tax applied','Tax rate is {0} percent for state {1}')}">
   <apex:param value="{!Tax__c.Rate}"/>
   <apex:param value="'{!Tax__c.StateName}'"/>                       
</apex:outputText>

This is probably the least painful method of getting this to work. Normally, in an apex:outputText, you can use '' to force a single ', but within an expression, it becomes apparently impossible.

| improve this answer | |
2

In a pinch, sometimes I just declare a variable (sq for single quote) to avoid making the syntax messier.

<apex:variable value="'" var="sq" />

<apex:outputText value="{!IF(Tax__c.Rate=='0','No state tax applied','Tax rate is {0} percent for state '+sq+'{1}'+sq)}">
   <apex:param value="{!Tax__c.Rate}"/>
   <apex:param value="{!Tax__c.StateName}"/>                       
</apex:outputText>

You could similarly do:

<apex:variable value='"' var='dq' />
| improve this answer | |
  • 1
    +1, but this technique has potential drawbacks if you do this inside a component or a page destined to be included into other pages. – sfdcfox Sep 4 '18 at 20:21
  • This is true - I've run into the clashing variable situation I assume you're alluding to. If your page or component has its own Apex controller you could also just make sq/dq controller variables. – Charles T Sep 4 '18 at 21:45

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.