How do I make control characters programatically in Javascript?

孤者浪人 提交于 2019-12-10 09:30:31

问题


In Javascript I can type '\u00A3' to get a character using its char code. I can do this programatically to with String.fromCharCode(parseInt('00A3', 16)).

But I can't find a way to do the same for a control character. I can type them in my source code but I want a way to generate them in code.


回答1:


Sounds to me like you could just use this list: http://en.wikipedia.org/wiki/C0_and_C1_control_codes and use the character points defined there to insert them with \u or String.fromCharCode as in your example?

PS: instead of the parseInt, you could use a literal: 0x00A3




回答2:


You can easily embed octal numbers:

var crlf = '\013' + '\012'; // octal numbers
alert('hello' + crlf + 'there'); // shows hello\n\rthere

Doesn't work the same for hex, though:

var clrf = '\0xD' + '\0xA'; // hex
alert('hello' + crlf + 'there'); // shows helloxDxAthere


来源:https://stackoverflow.com/questions/8386907/how-do-i-make-control-characters-programatically-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!