Jim*_*Jim 1384
"\n".charCodeAt(0);
Run Code Online (Sandbox Code Playgroud)
Moh*_*sen 363
String.prototype.charCodeAt()
可以将字符串字符转换为ASCII数字.例如:
"ABC".charCodeAt(0) // returns 65
Run Code Online (Sandbox Code Playgroud)
对于String.fromCharCode(10)
将数字转换为等于ASCII字符的相反用途.此函数可以接受多个数字并连接所有字符然后返回字符串.例:
String.fromCharCode(65,66,67); // returns 'ABC'
Run Code Online (Sandbox Code Playgroud)
这是一个快速的ASCII字符参考:
{
"31": "", "32": " ", "33": "!", "34": "\"", "35": "#",
"36": "$", "37": "%", "38": "&", "39": "'", "40": "(",
"41": ")", "42": "*", "43": "+", "44": ",", "45": "-",
"46": ".", "47": "/", "48": "0", "49": "1", "50": "2",
"51": "3", "52": "4", "53": "5", "54": "6", "55": "7",
"56": "8", "57": "9", "58": ":", "59": ";", "60": "<",
"61": "=", "62": ">", "63": "?", "64": "@", "65": "A",
"66": "B", "67": "C", "68": "D", "69": "E", "70": "F",
"71": "G", "72": "H", "73": "I", "74": "J", "75": "K",
"76": "L", "77": "M", "78": "N", "79": "O", "80": "P",
"81": "Q", "82": "R", "83": "S", "84": "T", "85": "U",
"86": "V", "87": "W", "88": "X", "89": "Y", "90": "Z",
"91": "[", "92": "\\", "93": "]", "94": "^", "95": "_",
"96": "`", "97": "a", "98": "b", "99": "c", "100": "d",
"101": "e", "102": "f", "103": "g", "104": "h", "105": "i",
"106": "j", "107": "k", "108": "l", "109": "m", "110": "n",
"111": "o", "112": "p", "113": "q", "114": "r", "115": "s",
"116": "t", "117": "u", "118": "v", "119": "w", "120": "x",
"121": "y", "122": "z", "123": "{", "124": "|", "125": "}",
"126": "~", "127": ""
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*eri 29
如果您只有一个字符而不是字符串,则可以使用:
'\n'.charCodeAt();
Run Code Online (Sandbox Code Playgroud)
省略0 ...
虽然它慢了.使用当前版本的chrome,它慢了5倍.
Fra*_*cia 23
虽然其他答案是对的,但我更喜欢这种方式:
function ascii (a) { return a.charCodeAt(0); }
Run Code Online (Sandbox Code Playgroud)
然后,使用它,只需:
var lineBreak = ascii("\n");
Run Code Online (Sandbox Code Playgroud)
我用它来做一个小的快捷方式系统:
$(window).keypress(function(event) {
if (event.ctrlKey && event.which == ascii("s")) {
savecontent();
}
// ...
});
Run Code Online (Sandbox Code Playgroud)
你甚至可以在map()或其他方法中使用它:
var ints = 'ergtrer'.split('').map(ascii);
Run Code Online (Sandbox Code Playgroud)
Fil*_*vić 16
对于那些想要获得字符串的所有ASCII代码的总和的人:
'Foobar'
.split('')
.map(x=>x.charCodeAt(0))
.reduce((a,b)=>a+b);
Run Code Online (Sandbox Code Playgroud)
或者,ES6:
[...'Foobar']
.map(char => char.charCodeAt(0))
.reduce((current, previous) => previous + current)
Run Code Online (Sandbox Code Playgroud)
小智 13
将字符串转换为UTF-8的数组(流):
const str_to_arr_of_UTF8 = new TextEncoder().encode("Adfgdfs");
// [65, 100, 102, 103, 100, 102, 115]
Run Code Online (Sandbox Code Playgroud)
注意:ASCII 是 UTF-8 的子集,因此这是通用解决方案
Ibr*_*wal 10
为确保完全 Unicode 支持和可逆性,请考虑使用:
'\n'.codePointAt(0);
Run Code Online (Sandbox Code Playgroud)
这将确保在测试超过 UTF-16 限制的字符时,您将获得它们的真实代码点值。
例如
''.codePointAt(0); // 68181
String.fromCodePoint(68181); // ''
''.charCodeAt(0); // 55298
String.fromCharCode(55298); // '?'
Run Code Online (Sandbox Code Playgroud)
JavaScript将字符串存储为UTF-16
(双字节),因此如果要忽略第二个字节,只需使用按位运算&
符0000000011111111
(即255)将其删除:
'a'.charCodeAt(0) & 255 === 97; // because 'a' = 97 0
'b'.charCodeAt(0) & 255 === 98; // because 'b' = 98 0
'?'.charCodeAt(0) & 255 === 19; // because '?' = 19 39
Run Code Online (Sandbox Code Playgroud)
使用方法charCodeAt
console.log("\n".charCodeAt())
Run Code Online (Sandbox Code Playgroud)
使用方法fromCharCode
console.log(String.fromCharCode(10))
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
830097 次 |
最近记录: |