I want to create a string in JavaScript that contains all ascii characters. How can I do this?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- how to split a list into a given number of sub-lis
Just loop the character codes and convert each to a character:
My javascript is a bit rusty, but something like this:
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.
Without doing several appends:
Here is a version in coffeescript
Just wanted to put this here for reference. (takes about 13/100 to 26/100 of a ms on my computer to generate).
Decomposed:
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).
strangely, this is only 3-10 times slower than assigning the string literal directly(with backticks to tell javascript to avoid most backslash parsing).
.