How to add long space between two words

Puneet4418
Tera Contributor

Hi,

How can I add a long space between two words in the script. Mentioned below is the code line where I am trying to add the space.

 

gr.comments = msg.getValue('message').toString()+' '+"-"+test;

 

Current output --> message -test

Required output --> message     -test

1 ACCEPTED SOLUTION

Astik Thombare
Tera Sage

Hi @Puneet4418 ,

 

Since you are dealing with a formatter field in ServiceNow, regular spaces or HTML entities may not work as expected. Instead, you can use a Unicode character for a non-breaking space or a different approach to ensure the spaces are visible. Here's a revised approach using Unicode non-breaking spaces:

 

 

 

var spaces = '\u00A0\u00A0\u00A0\u00A0\u00A0'; // 5 non-breaking spaces
gr.comments = msg.getValue('message').toString() + spaces + '-' + test;

 

 

 

If you need a longer space, you can adjust the number of \u00A0 accordingly. Alternatively, you can use a different Unicode character that provides a wider space, such as the em space (\u2003), which is wider than a regular space

 

You can repeat the em space character to achieve the desired length:var wideSpaces = '\u2003\u2003\u2003\u2003\u2003'; // 5 em spaces

 

 

 

gr.comments = msg.getValue('message').toString() + wideSpaces + '-' + test;

 

 

 

Using these Unicode characters should ensure that the spaces appear correctly in the formatter field in ServiceNow.

 

I checked it in my PDI and it is working fine 

 

Script -

 

 

AstikThombare_0-1717749635876.png

 

Result-

 

AstikThombare_1-1717749672179.png

 

  If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

                         By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

 

Astik

 

View solution in original post

3 REPLIES 3

Astik Thombare
Tera Sage

Hi @Puneet4418 ,

 

Method 1: Using Multiple Regular Spaces

 

This method is straightforward and works in most scripting languages:

 

 

gr.comments = msg.getValue('message').toString() + '        ' + "-" + test;

 

 

 

Replace ' ' with the desired number of spaces to create the long space. This approach is simple and reliable.

 

Method 2: Using a Non-Breaking Space Entity

 

This method utilizes a special HTML entity code to represent a non-breaking space, ensuring the space remains even if the line wraps:

 

 

gr.comments = msg.getValue('message').toString() + ' ' + "-" + test;

 

 

Here, ' ' represents a non-breaking space. This method can be useful when you need to control spacing across different environments.

 

 

gr.comments = msg.getValue('message').toString() + '     ' + '-' + test;

 

 

This will add five non-breaking spaces between the `message` and `-test`.

 

Alternatively, if you want a more customizable solution, you can use `String.prototype.repeat()` to specify exactly how many spaces you want:

 

 

var spaces = ' '.repeat(10); // Adjust the number of spaces as needed
gr.comments = msg.getValue('message').toString() + spaces + '-' + test;

 

 

This will add ten regular spaces between `message` and `-test`.

 

Choosing the Right Method:

  • If you only need a long space for visual formatting within your script, multiple regular spaces are sufficient.
  • If you require consistent spacing regardless of line wrapping or potential HTML rendering, the non-breaking space entity is a better choice.

Additional Considerations:

  • Some scripting languages might have built-in functions for adding long spaces or non-breaking spaces. Check your language's documentation for such functions.
  • Consider using a consistent approach for adding long spaces throughout your script for better readability and maintainability.

 

   If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

                         By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

 

Astik

Astik Thombare
Tera Sage

Hi @Puneet4418 ,

 

Since you are dealing with a formatter field in ServiceNow, regular spaces or HTML entities may not work as expected. Instead, you can use a Unicode character for a non-breaking space or a different approach to ensure the spaces are visible. Here's a revised approach using Unicode non-breaking spaces:

 

 

 

var spaces = '\u00A0\u00A0\u00A0\u00A0\u00A0'; // 5 non-breaking spaces
gr.comments = msg.getValue('message').toString() + spaces + '-' + test;

 

 

 

If you need a longer space, you can adjust the number of \u00A0 accordingly. Alternatively, you can use a different Unicode character that provides a wider space, such as the em space (\u2003), which is wider than a regular space

 

You can repeat the em space character to achieve the desired length:var wideSpaces = '\u2003\u2003\u2003\u2003\u2003'; // 5 em spaces

 

 

 

gr.comments = msg.getValue('message').toString() + wideSpaces + '-' + test;

 

 

 

Using these Unicode characters should ensure that the spaces appear correctly in the formatter field in ServiceNow.

 

I checked it in my PDI and it is working fine 

 

Script -

 

 

AstikThombare_0-1717749635876.png

 

Result-

 

AstikThombare_1-1717749672179.png

 

  If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

                         By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

 

Astik

 

SandraK
Tera Contributor

Hi @Puneet4418,

 

you can add a /t to your string: msg.getValue('message').toString()+'\t '+“-”+test;
This inserts a tab between the words.

 

Kind regards

Sandra