[SalesForce] Cannot convert Boolean to String

I have a need to show the boolean value as string and I'm trying to convert boolean to string but its not coming along, here is what I have done so far:

try to convert using String.valueOf but does not work.

tried this:

boolean b = true;
system.debug('boolean to string: ' + string.valueOf(b));

I'm getting the following:

USER_DEBUG [1]|DEBUG|boolean to string: true

But I want that to be in string like this 'true'

I have also tried to concatenate by using single quote

<apex:variable var="sr" value="{!string.valueOf('"' + record.status + '"')}"/>

then got this error:

Error Error: value="{!string.valueOf('"' + record.status + '"')}" EL
Expression Unbalanced: … {!string.valueOf('"' + record.status +
'"')} Error Error: EL Expression Unbalanced: … {!string.valueOf('"'
+ record.status + '"')}

Best Answer

If you really want to format it differently than what you get out of String.valueOf, it is probably worth creating a method for it.

public static String wrapSingleQuotes(Boolean input)
{
    return (input == null) ? '' : '\'' + String.valueOf(input) + '\'';
}

Then you can do:

system.debug('boolean to string: ' + wrapSingleQuotes(b));

Same strategy would apply for parens, double quotes, etc.

In VF, you can simply use outputText:

<apex:outputText value="{!'\'' & myBoolean & '\''}" />