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
pjaenickepjaenicke 

Preserve whitespace in a string

Greetings Listers,

 

Within an Apex class, I'm looping through and building a string thusly :

 

  mystring += eachrecord.Quantity__c + '         ' + eachrecord.Item__c + '\n';

 

After setting the target field to this value, when displaying this value in a report (or an alert), the whitespace between the Quantity and Item values is compressed to a single space.  How can I prevent this?

 

Thanks,

Pete

Best Answer chosen by Admin (Salesforce Developers) 
pjaenickepjaenicke

Solved this one...  here's how :

 

ASCII code 160, (more affectionately known as '&nbsp' in HTML) is what I'm trying to insert into my string.

 

Opened up Excel, in cell [A1] I entered the formula =code(160).  In cell [B1], I entered the formula "=A1&A1&A1&A1".  I then copied/pasted the contents of [B1] into my Apex string.

 

Crude, but effective.

 

Thanks,

Pete

All Answers

DharmeshDharmesh

You can use html encoding for white space.

Try this

 

mystring += eachrecord.Quantity__c + '     ' + eachrecord.Item__c + '\n';

SSRS2SSRS2

Hello pete;

   You can preserve white space by applying style in VF page component, style="white-space:pre;"

for more info refer Remove middle spaces

Ex:

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

 

-Suresh

 

pjaenickepjaenicke

Thanks Dharmesh -

 

This didn't work...  the report output appears as :

 

Mystring

123 &nbsp;&nbsp;&nbsp; Sample Item 1
456 &nbsp;&nbsp;&nbsp; Sample Item 2
789 &nbsp;&nbsp;&nbsp; Sample Item 3

 

Suresh - regarding your idea - though I'm using Apex code to build the string, I'm not using a Visual Force page.  I'm just trying to build a string, store the value in a custom field, ("Mystring"), and either allow users to display the field in their own reports, or use the field output in an e-mail alert.

 

Any other ideas?

 

Thanks again,

Pete

pjaenickepjaenicke

Solved this one...  here's how :

 

ASCII code 160, (more affectionately known as '&nbsp' in HTML) is what I'm trying to insert into my string.

 

Opened up Excel, in cell [A1] I entered the formula =code(160).  In cell [B1], I entered the formula "=A1&A1&A1&A1".  I then copied/pasted the contents of [B1] into my Apex string.

 

Crude, but effective.

 

Thanks,

Pete

This was selected as the best answer
SSRS2SSRS2

Hello 

 

<apex:outputText value="{!apexMethod}" style="white-space:pre;"></apex:outputText>

Or

<apex:outputText value="{!obj.customField}" style="white-space:pre;"></apex:outputText>

 

pjaenickepjaenicke

Hi Suresh,

 

For this purpose, I had no need to use VisualForce at all, just using Apex to build a text field on a parent object based on the value(s) in it's associated child objects. 

 

I'm subsequently able to reference that field using "standard" Salesforce workflow (e-mail alerts) and reporting.

 

Cut/pasting several ASCII 160's into my string was all that was needed.

 

Thanks,

Pete

nasknask

hi i have a similar requirement could your solution help?

 

please see my requirement , if yes then what do i need to do in my case?

Scenario:
Our client have a third party which accepts a text document and processes it. I am able to successfully create a text attachment. But I am facing a silly problem while creating text inside it and I am not able to get around it.
 
The 3rd party reads this Text document as a set of strings. i.e. for ex: 1st 10 characters would be First name and 2nd 10 characters is second name and goes on..... if in this case if I want to send a simple text 'ASHOK KUMAR' then text attachment should be 'ASHOK(5 Spaces)KUMAR(5SPACES)' .this 5 spaces would be derived dynamically as in case of 'RAVI HEER' it can be 6 spaces after each string and this goes on.
I am not able to achieve this particular bit of adding these extra bit of spaces between two strings or end of a string.
 
Methods which I tried but didn't worked :
1) I tried using a for loop and tried adding blank spaces it didn't worked, at the end it always gave me one space between two strings.
2) tried using '&nbsp' but it got printed as it is, it didn't worked if we use '\n' it created new line not sure how this worked.
3) I tried inserting some symbols like '@' between strings in first go and then tried to replacing these inserted symbols '@' by spaces but even this didn't worked it ended up in single space at last.
 
So if anybody has any other idea then please let me know.
pjaenick.ax736pjaenick.ax736
Hi Nask - sorry for the very late response - hopefully you've gotten this working already, but...

The solution above should work for you too.  Create a string outside of SF (using Excel or similar), consisting of five ascii code 160s (hex 'A0'), then paste-in that string into SF in place of your white-space.  To you and I, "     " looks just like "     ", but the first is using the non-breaking space character and will be rendered as such, whereas the latter is just the plain-ole space, and will be compressed.