String that contains all ascii characters

前端 未结 7 1287
灰色年华
灰色年华 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:38

    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.

提交回复
热议问题