String that contains all ascii characters

前端 未结 7 1293
灰色年华
灰色年华 2021-02-03 23:01

I want to create a string in JavaScript that contains all ascii characters. How can I do this?

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-03 23:50

    My javascript is a bit rusty, but something like this:

    s = '';
    for( var i = 32; i <= 126; i++ )
    {
        s += String.fromCharCode( i );
    }
    

    Not sure if the range is correct though.

    Edit:
    Seems it should be 32 to 127 then. Adjusted.

    Edit 2:
    Since char 127 isn't a printable character either, we'll have to narrow it down to 32 <= c <= 126, in stead of 32 <= c <= 127.

提交回复
热议问题