Hi,
I want to create a string in JavaScript that contains all ascii characters. How can I do this?
Hi,
I want to create a string in JavaScript that contains all ascii characters. How can I do this?
Python 2.6+:
import json
print json.dumps(''.join(chr(x) for x in range(32, 127)))
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.
Just loop the character codes and convert each to a character:
var s = '';
for (var i=32; i<=127;i++) s += String.fromCharCode(i);