19

Is there a better way to add x amount of white space to a string?

str = "blah";
x = 4;

for (var i = 0; i < x; i++){
  str = ' ' + str;
}

return str;
9
  • Define 'better.' Incidentally: stackoverflow.com/questions/14343844/… Commented Sep 14, 2014 at 0:12
  • If performance matters, you're doing a lot of unnecessary string concatenations when x becomes high. Imagine you wanted 512 spaces. Concatenating two strings of 256 spaces would be faster than prepending a space 512 times. On my phone but this is usually called left and right padding of strings. Commented Sep 14, 2014 at 0:13
  • no point having a loop if you are always adding the same amount of padding Commented Sep 14, 2014 at 0:14
  • There's a "padding function" in web browsers. What are you trying to achieve? Why do you want those spaces? Commented Sep 14, 2014 at 0:15
  • 1
    Have a look at the String.repeat method: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 14, 2014 at 0:17

6 Answers 6

36

Could do it like this, prevents the loop.

str = str + new Array(x + 1).join(' ')

Sign up to request clarification or add additional context in comments.

6 Comments

This gives a length of x - 1 because join takes the element at right and the last element does not have one. So it should be new Array(x + 1).join(' ')
Also, it should be str = new Array(x+1).join(' ') + str;. Your version pads on the wrong side.
Durp, you are correct this pads to the right. I didn't read the question properly. Guess its to late in the night for me.
it is all good, i figured out to put it on the other side, just waiting for time to run out to give answer
Yeah definitely, I just pointed it out since @aduch also pointed something out. Very elegant solution by the way.
|
27

In ES6 you can do the following:

str = ' '.repeat(x) + str;

At this point in time (late 2014) it's only available in Chrome and Firefox. But in two years it should be widely supported.

See the documentation for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat

Comments

2

Alternatively using lodash _.padStart. Pads string on the left side if it's shorter than length.

const str = 'blah',
  len = str.length,
  space = 4;
console.log(_.padStart(str, len + space));
// => '    blah'
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

Or pure JavaScript:

const str = 'blah',
  len = str.length,
  space = 4;
console.log(str.padStart(len + space, ' '));

Comments

1

for example you can use repeat for the white space left or right of your string:

var j = 6;
for (i = 0; i < n; i++) {
    console.log(" ".repeat(j-1)+"#".repeat(i+1))
    j--;
}

Comments

1

You can use padStart and padEnd methods. For eg.:

const str1 = 'Hello';
const str2 = 'World';
const str3 = str1.padEnd(2,' ')+str2.padStart(1,' ');
console.log(str3);

1 Comment

.padStart() with whitespace as fill string not working foe me
1
var message = 'stack' + Array(6).fill('\xa0').join('') + 'overflow'
console.log(message);

var message = 'stack' + Array(6).fill('\xa0').join('') + 'overflow'
console.log(message);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.