I want to create a string in JavaScript that contains all ascii characters. How can I do this?
This is a version written in python. Gives all ASCII characters in order as a single string.
all_ascii = ''.join(chr(k) for k in range(127)) # 7 bits
all_chars = ''.join(chr(k) for k in range(255)) # 8 bits
printable_ascii = ''.join(chr(k) for k in range(127) if len(repr(chr(k))) == 3)
>>> print(printable_ascii)
' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~'
The last string here, printable_ascii
contains only those characters that contain no escapes (i.e. have length == 1). The chars like: \x05, \x06
or \t, \n
which does not have its own glyph in your system's font, are filtered out.
len(repr(chr(k))) == 3
includes 2 quotes that come from repr
call.