String that contains all ascii characters

50,200

Solution 1

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.

Solution 2

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

Solution 3

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

var s = '';
for (var i=32; i<=127;i++) s += String.fromCharCode(i);

Solution 4

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);

.

Solution 5

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(128))  # 7 bits
all_chars = ''.join(chr(k) for k in range(256))  # 8 bits
printable_ascii = ''.join(chr(k) for k in range(128) 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.

Share:
50,200
Gjorgji
Author by

Gjorgji

Updated on July 09, 2022

Comments

  • Gjorgji
    Gjorgji almost 2 years

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

  • Michael Petrotta
    Michael Petrotta about 14 years
    He did specify ASCII, and likely wants only printable characters, so you'll want to stick to 32 <= c <= 127.
  • Ignacio Vazquez-Abrams
    Ignacio Vazquez-Abrams about 14 years
    Actually, character 127 isn't really printable, so 32 <= c < 127.
  • Decent Dabbler
    Decent Dabbler about 14 years
    You are right Michael, I wasn't sure about the actual range of ASCII. Shame on me. :-/ But I've looked it up now, and it seems we should narrow it down to 32 to 126, since DEL is not a printable character either I believe. But correct me if I'm wrong.
  • Decent Dabbler
    Decent Dabbler about 14 years
    @Ignacio: Well, there you go, you beat me to it.
  • Gjorgji
    Gjorgji about 14 years
    I need all ascii chatacters. However, this helps, too. Thanks
  • Joachim Sauer
    Joachim Sauer about 14 years
    @Gjorgji: which characters are you missing? Why do you think that those are not all?
  • Michael Petrotta
    Michael Petrotta about 14 years
    @Joachim: I'll bet he's looking for "all printable Western European characters" or "all printable characters in my native script". The definition of the word "ascii", especially when rendered in lowercase, has gotten a bit sloppy over the years, I've noticed.
  • Albert Hendriks
    Albert Hendriks almost 8 years
    I was looking for unicode, so someone else might also be: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzÀÁÂÃÄÅÆÇ‌​ÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜ‌​ÝÞßàáâãäåæçèéêëìíîïð
  • user3479901
    user3479901 almost 8 years
    @Albert Hendriks That does not contain all Unicode code points. It doesn't even cover the first two blocks.
  • user3479901
    user3479901 almost 8 years
    This question very clearly asks for a string containing all (printing) ASCII characters, which is feasible, as ASCII is a small, fixed set. If you're intending to cover any set of characters beyond ASCII, you must understand how Unicode classifies code points and use the proper solution for your problem case. If you're looking for "a string that contains all characters", you're doing things wrong - most likely, something very wrong.
  • Albert Hendriks
    Albert Hendriks almost 8 years
    I'm just trying to help anyone who might be looking for some unicode characters, like I was myself. Got here from Google.
  • YakovL
    YakovL over 5 years
    There is also Extended ASCII with charcodes up to 255 (includes some accented letters like á, various symbols like ±, º, ≡ and also nbsp with the code 255)
  • Tom Blodget
    Tom Blodget over 5 years
    @YakovL Extended ASCII isn't one character set. So the term is insufficient for almost all purposes. JavaScript uses the UTF-16 character encoding of the Unicode character set (as does Java, .NET, VB4/5/6/A/Script, …).
  • Bob Stein
    Bob Stein almost 5 years
    I just wanted a string literal with all ASCII printable characters. Thanks google. Thanks @StuartP.Bentley.
  • MilkyWay90
    MilkyWay90 over 4 years
    The question asked for printable ASCII.