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

How to create string of blanks (" ") in javascript or jQuery? - Stack Overflow

programmeradmin1浏览0评论

I have an array that contains 10 elements, each element contains " ".

How do I create a string of spaces, like this:

"          "

in javascript or jQuery from the this array?

Thank you

I have an array that contains 10 elements, each element contains " ".

How do I create a string of spaces, like this:

"          "

in javascript or jQuery from the this array?

Thank you

Share Improve this question edited Jul 23, 2010 at 11:12 Nick Craver 630k138 gold badges1.3k silver badges1.2k bronze badges asked Jul 23, 2010 at 11:06 user338195user338195 6
  • There should be 10 blanks in the brackets, stack overflow has replaced it with a single blank. – user338195 Commented Jul 23, 2010 at 11:07
  • I'm not quite sure what you want. Maybe show the result. You could use another character such as _ to make it clearer. – RoToRa Commented Jul 23, 2010 at 11:11
  • @RoToRa - A <pre> block will do the job, I updated the question :) – Nick Craver Commented Jul 23, 2010 at 11:12
  • @Nick: darn, I put a lot of effort into replacing those spaces with &nbsp;! ;-) – Andy E Commented Jul 23, 2010 at 11:15
  • @Andy - Woops, didn't see you were editing as well..that'll teach you not to put any effort into improving questions! – Nick Craver Commented Jul 23, 2010 at 11:18
 |  Show 1 more comment

5 Answers 5

Reset to default 24

Easy, try it yourself in address box:

javascript:alert('“'+new Array(42).join(' ')+'”')

By the way, "in jquery" should be "using jquery"

You would use Array.join() for this, like this:

var myArray = [" "," "," "," "," "," "," "," "," "," "];
var myString = myArray.join(''); //mySting is a string of 10 spaces

You need to pass the '' to .join() because the default joiner is a comma.

Try it with a padEnd() string method:

let res = "".padEnd(10, " ");
console.log('start' + res + 'end');

You can use join for that. Example:

var x = ['a', 'b', 'c', 'd'];
var y = x.join('');

Try it with a fill() method:

let res = Array(10).fill(' ').join('')
console.log('start' + res + 'end')

发布评论

评论列表(0)

  1. 暂无评论