Skip to content
Related Articles

Related Articles

Create a string with multiple spaces in JavaScript
  • Last Updated : 10 May, 2019

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  .




<!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>

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




<!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>

Output:

  • Before clicking on the button:
  • After clicking on the button:
My Personal Notes arrow_drop_up
Recommended Articles
Page :