Javascript 包含所有 ascii 字符的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2444447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String that contains all ascii characters
提问by Gjorgji
I want to create a string in JavaScript that contains all ascii characters. How can I do this?
我想在 JavaScript 中创建一个包含所有 ascii 字符的字符串。我怎样才能做到这一点?
采纳答案by Decent Dabbler
My javascript is a bit rusty, but something like this:
我的javascript有点生疏,但像这样:
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.
编辑:然后
似乎应该是 32 到 127。调整。
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.
编辑 2:
由于 char 127 也不是可打印字符,我们必须将其缩小到 32 <= c <= 126,而不是 32 <= c <= 127。
回答by Stuart P. Bentley
var s = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
回答by Guffa
Just loop the character codes and convert each to a character:
只需循环字符代码并将每个代码转换为一个字符:
var s = '';
for (var i=32; i<=127;i++) s += String.fromCharCode(i);
回答by Dmitry
Just wanted to put this here for reference. (takes about 13/100 to 26/100 of a ms on my computer to generate).
只是想把它放在这里以供参考。(在我的计算机上生成大约需要 13/100 到 26/100 毫秒)。
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).
最有效的解决方案(如果您确实想在每次脚本运行时生成整个集合,可能是)(在我的计算机上生成大约需要 3/100-35/100 毫秒)。
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).
奇怪的是,这仅比直接分配字符串文字慢 3-10 倍(使用反引号告诉 javascript 避免大多数反斜杠解析)。
var x;
var t;
t = performance.now();
x = '!\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~';
t = performance.now() - t;
console.log(t);
.
.
回答by Bruno Lopes
Without doing several appends:
不做几个附加:
var s = Array.apply(null, Array(127-32))
.map(function(x,i) {
return String.fromCharCode(i+32);
}).join("");
回答by Dinis Cruz
Here is a version in coffeescript
这是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)

