\x Escape in Java?

限于喜欢 提交于 2019-12-17 16:24:25

问题


I was wondering if there is a similar hex (\x) escape in Java like there is in C++. For example:

char helloworld[] = "\x48\x45\x4C\x4C\x4F\x20\x57\x47\x52\x4C\x44";
printf("%s", helloworld);

There is no hex (\x) escape in Java from what it appears so far. Is there an alternative that is just as easy to use without having to concat a bunch of hex numbers together?


回答1:


Strings in Java are always encoded in UTF-16, so it uses a Unicode escape: \u0048. Octal characters are supported as well: \110




回答2:


Note that Unicode escapes are parsed quite early. It can come as a surprise when

  String s = "text\u000d\u000a";

causes a compiler error because you should have used "text\015\012" or "text\r\n"



来源:https://stackoverflow.com/questions/3613759/x-escape-in-java

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