String that contains all ascii characters

前端 未结 7 1289
灰色年华
灰色年华 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:36
    var s = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-03 23:43

    Just wanted to put this here for reference. (takes about 13/100 to 26/100 of a ms on my computer to generate).

    var allAsciiPrintables = JSON.stringify((Array.from(Array(126 + 32).keys()).slice(32).map((item) => {
        return String.fromCharCode(item);
    })).join(''));
    

    Decomposed:

    var allAsciiPrintables = (function() {
        /* ArrayIterator */
        var result = Array(126 + 32).keys();    
        /* [0, 126 + 32] */
        result = Array.from(result);
        /* [32, 126 + 32] */
        result = result.slice(32);
        /* transform each item from Number to its ASCII as String. */
        result = result.map((item) => {
            return String.fromCharCode(item);
        });
        /* convert from array of each string[1] to a single string */
        result = result.join('');
    
        /* create an escaped string so you can replace this code with the string 
           to avoid having to calculate this on each time the program runs */
        result = JSON.stringify(result);
    
        /* return the string */
        return result;
    })();
    

    The most efficient solution(if you do want to generate the whole set each time the script runs, is probably)(takes around 3/100-35/100 of a millisecond on my computer to generate).

    var allAsciiPrintables = (() => {
        var result = new Array(126-32);
        for (var i = 32; i <= 126; ++i) {
            result[i - 32] = (String.fromCharCode(i));        
        }
        return JSON.stringify(result.join(''));
    })();
    

    strangely, this is only 3-10 times slower than assigning the string literal directly(with backticks to tell javascript to avoid most backslash parsing).

    var x;
    var t;
    
    t = performance.now();
    x = '!\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
    t = performance.now() - t;
    console.log(t);
    

    .

    0 讨论(0)
  • 2021-02-03 23:43

    Here is a version in coffeescript

    require 'fluentnode'
    
    all_Ascii = ->
      (String.fromCharCode(c) for c in  [0..255])
    
    describe 'all Ascii', ->
    
      it 'all_Ascii', ->
        all_Ascii.assert_Is_Function()
        all_Ascii().assert_Size_Is 256
        all_Ascii()[0x41].assert_Is 'A'
        all_Ascii()[66  ].assert_Is 'B'
        all_Ascii()[50  ].assert_Is '2'
        all_Ascii()[150 ].assert_Is String.fromCharCode(150)
    
    0 讨论(0)
  • 2021-02-03 23:44

    Without doing several appends:

    var s = Array.apply(null, Array(127-32))
      .map(function(x,i) {
        return String.fromCharCode(i+32);
      }).join("");
      document.write(s);

    0 讨论(0)
  • 2021-02-03 23:45

    Just loop the character codes and convert each to a character:

    var s = '';
    for (var i=32; i<=127;i++) s += String.fromCharCode(i);
    
    0 讨论(0)
提交回复
热议问题