You need to sign in to do that
Don't have an account?

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
Solved this one... here's how :
ASCII code 160, (more affectionately known as ' ' 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
You can use html encoding for white space.
Try this
mystring += eachrecord.Quantity__c + ' ' + eachrecord.Item__c + '\n';
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:
-Suresh
Thanks Dharmesh -
This didn't work... the report output appears as :
Mystring
123 Sample Item 1
456 Sample Item 2
789 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
Solved this one... here's how :
ASCII code 160, (more affectionately known as ' ' 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
Hello pjaenicke
when you show output what component do you use.I just give a sample and you can use output text with your custum field or apex method also
Or
-Suresh
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
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?
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.