JavaScript 共有 6 种方法可以表示一个字符, Java 呢?方便输入一些特殊字符

2020-08-22 13:02:28 +08:00
 lxk11153

有了这种表示法之后,JavaScript 共有 6 种方法可以表示一个字符。

'\z' === 'z'  // true
'\172' === 'z' // true
'\x7A' === 'z' // true
'\u007A' === 'z' // true
'\u{7A}' === 'z' // true
1765 次点击
所在节点    Java
8 条回复
lxk11153
2020-08-22 13:42:05 +08:00
echo -e "\x7A"
lxk11153
2020-08-22 13:43:56 +08:00
echo -e "\172"
ChanKc
2020-08-22 14:45:14 +08:00
Java 可以进行 byte 层面的操作啊,还支持很多种编码格式。
lxk11153
2020-08-22 15:54:05 +08:00
@ChanKc #3 举个例子?
passerbytiny
2020-08-22 15:58:27 +08:00
Literal 的表示方式,只有 "z" 是字符串 z,'z' 都不行(那是 char 类型的 z )。
passerbytiny
2020-08-22 16:12:04 +08:00
如果用编程的方式来获取,那真得是很多,比如 String.valueOf(new char[] {122, 0172, 0x7A})等价与 "zzz" 。
lxk11153
2020-09-08 15:13:27 +08:00
补充 #2 https://www.gnu.org/software/coreutils/manual/html_node/echo-invocation.html

‘\0nnn’
the eight-bit value that is the octal number nnn (zero to three octal digits), if nnn is a nine-bit value, the ninth bit is ignored

‘\nnn’
the eight-bit value that is the octal number nnn (one to three octal digits), if nnn is a nine-bit value, the ninth bit is ignored

‘\xhh’
the eight-bit value that is the hexadecimal number hh (one or two hexadecimal digits)
lxk11153
2020-09-08 19:15:08 +08:00
Java: https://www.runoob.com/java/java-basic-datatypes.html
\ddd 八进制字符 (ddd)
\uXXXX 16 进制 Unicode 字符 (xxxx)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/700491

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX