Search code examples
javascriptstringascii

String that contains all ascii characters


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


Solution

  • 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.