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
nasknask 

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.

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

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

Baktash H.Baktash H.

String sf = 'Salesforce';

sf = '          ' + sf + '          ';

 

how about this?

 

Rahul SharmaRahul Sharma

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.

sfdcfoxsfdcfox

Rahul, you would be incorrect. Whitespace is not ignored or collapsed. See this example that proves this:

 

public class test {
    public String getTest() {
        return '123456789012345678901234567890\n     TEST     TEST  TEST';
    }
}

 

<apex:page controller="test" >
    <pre>
{!test}
    </pre>
</apex:page>

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:

 

    public String leftpad(String source, integer maxlength) {
        if(source==null) return null;
        if(source.length()>maxlength) return source;
        return '                                                                                ' .substring(0,maxlength-source.length()) + source;
    }

    public String rightpad(String source, integer maxlength) {
        if(source==null) return null;
        if(source.length()>maxlength) return source;
        return source + '                                                                                ' .substring(0,maxlength-source.length());
    }

These functions will correctly pad a string with up to 80 characters of whitespace.

nasknask

hi both,

my requirement in details is below.

 

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.

 

 

nasknask

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:

 

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.

 

nasknask

Thanks for the reply this doesnt work. You cant just add spaces in salesforce in that fashion.

sfdcfoxsfdcfox

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 &nbsp; 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 was selected as the best answer
nasknask

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);

nasknask

Thanks SFDCFOX i used your two method to insert fixed number of spaces :) thanks for that.