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
SSRS2SSRS2 

Remove middle spaces

I want to get values with middle spaces but only show one space in output (Salesforce.com API version 19.0).

VF page:

<apex:page controller="TestSpace">
<apex:outputText value="A B"></apex:outputText><br/>
<apex:outputText >A C</apex:outputText><br/>
<apex:outputText value="{!fromController}"></apex:outputText><br/>
<apex:outputText >A E</apex:outputText><br/>
</apex:page>

 Apex Class:

public class TestSpace {
public String getFromController() {
return 'A D';
}
}

Output:

  A B  //not expected

  A C //not expected

  A D //not expected

  A E //ok

 

-Suresh


Best Answer chosen by Admin (Salesforce Developers) 
SSRS2SSRS2

 

With style="white-space:pre;" could obtain the output with middle spaces.

<apex:outputText value="A         B" style="white-space:pre;"></apex:outputText>

 

-Suresh

All Answers

bob_buzzardbob_buzzard

I'm slightly confused, as the output you have marked as unexpected has a single space between the first and second value, which I though was what you wanted.

 

Anyhow, this is a browser function - browsers collapse repeated whitespace into a single space.  If you want multiple spaces, you have to use non-breaking spaces (&nbsp;) and tell the outputText component not to escape html by setting the escape attribute to false.

 

Can you explain a little more about what you are expecting to see?

SSRS2SSRS2

Thanks Bob.

 

It's working with '&nbsp;' and escape ‘false’ but is it ok when application need to do security review.

 

-Suresh

bob_buzzardbob_buzzard

I'm afraid I can't help with that. I don't see why the security review would worry about non-breaking spaces being unescaped.  That said, if it opens up a way for malicious HTML to appear, I would expect it to be a problem.

SSRS2SSRS2

 

With style="white-space:pre;" could obtain the output with middle spaces.

<apex:outputText value="A         B" style="white-space:pre;"></apex:outputText>

 

-Suresh

This was selected as the best answer