How to repeat whitespace

前端 未结 2 1086
感动是毒
感动是毒 2020-12-22 10:09

I am trying to get a string like the following one:

[==== ]

For that purpose I am using this code:



        
相关标签:
2条回答
  • 2020-12-22 10:49

    This is an issue of Chrome's developer tools. Empty spaces can be repeated, but they are displayed as one character, which is wrong:

    You can check the result like this:

    (' ' + ' ').length
    // => 2
    

    Here is an example from Node.js REPL:

    In conclusion, your code does work, but Google Chrome's console doesnt display the result correctly.

    If you're talking about the displaying in HTML elements that don't have monospace fonts, use @Jamiec's solution, using non-breaking spaces:

    '[' + '='.repeat (4) + ' '.repeat(4) + ']'
    

    As @James Thorpe this was a bug in Chrome which got fixed. Probably we will get the fix when updating the browser version.

    0 讨论(0)
  • 2020-12-22 10:57

    Browsers collapse multiple whitespace, in order to force it not to you can use non-breaking spaces.

    Therefore, in your case you can use

    ' '.repeat(4)
    

    (Its arguable that a debug console shouldn't collapse the whitespace, but in your case it seems it does)

    0 讨论(0)
提交回复
热议问题