Create a string with multiple spaces in JavaScript

We have a string with extra-spaces and if we want to display it in the browser then extra spaces will not be displayed. Adding the number of spaces to the string can be done in the following ways.

substr():
This method gets a part of a string, starts at the character at the defined position, and returns the specified number of characters.

Syntax:

string.substr(start, length)

parameters:

  • start:
    This parameter is required. It specifies the position from where to start the extraction. First index starts at 0.

    • If start parameter is positive and greater than, or equal, to the length of the provided string, this method will return an empty string.
    • If start parameter is negative, this method uses it as an index from the end of string.
    • If start parameter is negative or greater than the length of string, start is taken as 0.
  • length:This parameter is optional. It specifies the number of characters to extract. If not used, it extracts the whole string.

Return Value: Returns a new string, Which contains the extracted part of the text. If length is either 0 or negative, It will return an empty string.

Example-1:This example adds spaces to the string by &nbsp.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript 
      | Create a string with multiple spaces.
    </title>
</head>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" style="font-size: 16px;">
    </p>
    <button onclick="gfg_Run()">
        Add spaces
    </button>
    <p id="GFG_DOWN" 
       style="color:green;
              font-size: 20px; 
              font-weight: bold;">
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var string = 'A Computer Science Portal';
        el_up.innerHTML = string;
  
        function gfg_Run() {
            el_down.innerHTML = string.substr(0, 2) + 
              '          ' +
              string.substr(2);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example-2: This example adds spaces to the string by \xa0(it’s a NO-BREAK SPACE char).

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript 
      | Create a string with multiple spaces.
    </title>
</head>
  
<body style="text-align:center;" id="body">
    <h1 style="color:green;">  
            GeeksForGeeks  
        </h1>
    <p id="GFG_UP" style="font-size: 16px;">
    </p>
    <button onclick="gfg_Run()">
        Add spaces
    </button>
    <p id="GFG_DOWN"
       style="color:green;
              font-size: 20px;
              font-weight: bold;">
    </p>
    <script>
        var el_up = document.getElementById("GFG_UP");
        var el_down = document.getElementById("GFG_DOWN");
        var string = 'A Computer Science Portal';
        el_up.innerHTML = string;
  
        function gfg_Run() {
            el_down.innerHTML = string.substr(0, 18) +
              '\xa0\xa0\xa0\xa0\xa0\xa0\xa0 ' +
              string.substr(18);
        }
    </script>
</body>
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.