最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to make space between strings using concat() method in JavaScript - Stack Overflow

programmeradmin4浏览0评论

I want to concat multiple string using concat() method it works fine but how can i make white space between them

<HTML>
    <head>
       <title></title>
    </head>
    <body>
       <label id="label_one" style="color:blue;"></label><br/>
       <label id="label_two" style="color:blue;"></label><br/>
       <label id="label_three" style="color:blue;"></label><br/>
       <script>
           var one = "abc";
           var two = "def";
           var three = one.concat(two)
           var four = three.length;
           var variableLen = one.concat(three, four);
           document.getElementById("label_one").innerHTML = one;
           document.getElementById("label_two").innerHTML = two;
           document.getElementById("label_three").innerHTML = variableLen;
       </script>
    </body>
</HTML>

and my output is

abcdef6 I want my output like this abc def 6 with space after concate and using concate() method of JavaScript not with concate operator(+)

I want to concat multiple string using concat() method it works fine but how can i make white space between them

<HTML>
    <head>
       <title></title>
    </head>
    <body>
       <label id="label_one" style="color:blue;"></label><br/>
       <label id="label_two" style="color:blue;"></label><br/>
       <label id="label_three" style="color:blue;"></label><br/>
       <script>
           var one = "abc";
           var two = "def";
           var three = one.concat(two)
           var four = three.length;
           var variableLen = one.concat(three, four);
           document.getElementById("label_one").innerHTML = one;
           document.getElementById("label_two").innerHTML = two;
           document.getElementById("label_three").innerHTML = variableLen;
       </script>
    </body>
</HTML>

and my output is

abcdef6 I want my output like this abc def 6 with space after concate and using concate() method of JavaScript not with concate operator(+)

Share Improve this question edited Mar 28, 2018 at 8:14 kakabali 4,0332 gold badges33 silver badges60 bronze badges asked Mar 28, 2018 at 7:18 RaheelRaheel 581 gold badge1 silver badge7 bronze badges 5
  • Not sure why your answer was voted down, other than perhaps the info could have been found elsewhere. In any case, see answer below – Zach Smith Commented Mar 28, 2018 at 7:23
  • var one = "abc "; Just give space at the end of your string values and then do the concatenation. This is one way – Arunachalam E Commented Mar 28, 2018 at 7:24
  • var three = one.concat(' ', two) .... var variableLen = one.concat( ' ', three, ' ', four); – Jaromanda X Commented Mar 28, 2018 at 7:27
  • Actually i want to concat variable if u notice my question my var with the name of three = one.concat(two) at this point value of variable three in abcdef then i am using length properties and value of variable three is length of string then i concat value of variable one with variable three whose value after using concat() is abcdef with variable four whose value after using length property is 6 how could i make space between them if i am using concat operator(+) then simply give space between them but how in concat() method i do the same – Raheel Commented Mar 28, 2018 at 7:33
  • @Raheel. concat() takes a variable number of arguments as at least two answers show below. if you want the length of three to exclude spaces from concatenated strings, then either work out the strings lengths independently and add together. Or strip the spaces from the string when finding a length: var four = three.replace(/\s/g, '').length – Zach Smith Commented Mar 28, 2018 at 7:38
Add a comment  | 

5 Answers 5

Reset to default 10

Try this [one,two].join(' '); (Adapt to your needs as necessary, i.e. build an array of strings, then use join)

One way of doing it is:

var one = "abc";
var two = "def";
var three = one.concat(" ", two);
var four = three.length;
var variableLen = one.concat(" ", two, " ", four);

However it might be better to use join as another answer mentions. On a side note, you might notice that the concat() function takes a variable number of arguments. You can achieve this in your own functions like this:

function func1() {
  console.log(arguments); // => [1,2,3]
}

func1(1, 2, 3);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments

Simply add the white space to the concat. I prefer to use '&nbsp' to generate the space.

<HTML>
    <head>
       <title></title>
   </head>
   <body>
     <label id="label_one" style="color:blue;"></label><br/>
     <label id="label_two" style="color:blue;"></label><br/>
     <label id="label_three" style="color:blue;"></label><br/>
    <script>
      var one = "abc";
      var two = "def";
      var three     = one.concat(two)
      var four= three.length;
      var variableLen = one.concat('&nbsp', three, four);
      document.getElementById("label_one").innerHTML = one;
      document.getElementById("label_two").innerHTML = two;
      document.getElementById("label_three").innerHTML = variableLen;
     </script>
  </body>
</HTML>

You can try the following:

<HTML>
    <head>
       <title></title>
   </head>
   <body>
     <label id="label_one" style="color:blue;"></label><br/>
     <label id="label_two" style="color:blue;"></label><br/>
     <label id="label_three" style="color:blue;"></label><br/>
    <script>
      var one = "abc";
      var two = "def";
      var whiteSpace = " ";
      var three     = one.concat(whiteSpace, two)
      var four= three.length;
      var variableLen = one.concat( three,whiteSpace ,four);
      document.getElementById("label_one").innerHTML = one;
      document.getElementById("label_two").innerHTML = two;
      document.getElementById("label_three").innerHTML = variableLen;
     </script>
  </body>
</HTML>

try this

var one = "abc";
  var two = "def";
  var three     = one.concat(" ",two)
  var four= three.length;
  var variableLen = one.concat(" ", three," ", four);
发布评论

评论列表(0)

  1. 暂无评论