You need to sign in to do that
Don't have an account?
nask
insert predefined amount of balnk spaces between word or before a word.
Hi i have a requirement where i need to add a fixed amount of spaces before a Word. for example we have a word 'SALESFORCE' then i need to add fixed amount of space i.e 10 spaces before it or after it like " Salesforce" or "SALESFORCE ". How could i achieve this?
This requirement is for creating a text attachememnt where our third party reads this and does its work.
My example from before does work.
The important thing to note here is that you are not facing a Visualforce or Apex Code limitation, but instead just an ordinary HTML feature. Visualforce does "help" you by automatically HTML-escaping strings normally. This is why didn't work.
Use <apex:outputText value="{!value}" escape="false" /> to emit &nsbp; as literal HTML code in Visualforce. This will retain the spaces as you expect. Alternatively, use the pre tag, or use style="white-space: pre" on any output element that supports the style attribute. See my example code for how this works.
All Answers
String sf = 'Salesforce';
sf = ' ' + sf + ' ';
how about this?
Hi Nask,
As far as i know adding multiple whitespaces in between 2 string in apex is not possible.
Even if you try to add multiple white space, it will still consider single.
Rahul, you would be incorrect. Whitespace is not ignored or collapsed. See this example that proves this:
Just remember that HTML will collapse whitespace by default (I used pre to force HTML to preserve the whitespace to demonstrate). Just because it doesn't look right when rendered on a page doesn't mean the spaces are not there.
If you are trying to align to columns, there isn't a library function that will provide automatic whitespace padding (as you might do with printf in C). You'll have to write this function yourself. You can use something like this:
These functions will correctly pad a string with up to 80 characters of whitespace.
hi both,
my requirement in details is below.
Thanks first of all to giving me this idea.
i tried your code but it doesnt work in this case it iserts just 1 space for my example i amdeded your code sme thing like this:
String source = 'Ashok';
integer maxlength = 10;
system.debug('ask'+ ' ' .substring(0,maxlength-source.length()) + source);
it ended up in
ASK ASHOK it didnt inserted 80 as suggested by you.
To be in detail my requirement is something like this:
Thanks for the reply this doesnt work. You cant just add spaces in salesforce in that fashion.
My example from before does work.
The important thing to note here is that you are not facing a Visualforce or Apex Code limitation, but instead just an ordinary HTML feature. Visualforce does "help" you by automatically HTML-escaping strings normally. This is why didn't work.
Use <apex:outputText value="{!value}" escape="false" /> to emit &nsbp; as literal HTML code in Visualforce. This will retain the spaces as you expect. Alternatively, use the pre tag, or use style="white-space: pre" on any output element that supports the style attribute. See my example code for how this works.
This is solved you can insert required number of spaces by using method: string.format()
You can execute this code to see hoe this works:
how can we use this method .
// Create a Template String, that has tokens of form {index}
String templateString =
'Insert 10 spaces .Here you go..'
;
// String argument list or array matching each {index} in Template String.
String[] arguments =
new
String[] {
'Ta DA'
,
'i love this method'
};
// Call String.format() to get the token replaced
String formattedString = String.format(templateString, arguments);
System.debug(formattedString);
Thanks SFDCFOX i used your two method to insert fixed number of spaces :) thanks for that.