如何在Java源文件中输入多个Unicode字符串(包括从右到左的阅读顺序)?
我正在测试一段 Java 代码,需要创建一个字符串数组。这些字符串是不同语言中的单词,包括阿拉伯语等从右到左阅读顺序的单词(不知道这是否重要......)
所以我需要做这样的事情:
ArrayList<String> words = ...
words.add(<word-in-english>);
words.add(<word-in-chinese>);
words.add(<word-in-russian>);
words.add(<word-in-arabic>);
将这些放入的最佳方式是什么我的Java代码?除了对字符串中的每个字符使用“\u”转义之外,还有其他方法吗?谢谢
I am testing a piece of Java code and need to create an array of strings. These strings are words in different languages, including those like Arabic with the right-to-left reading order (don't know if that matters...)
So I need to do something like this:
ArrayList<String> words = ...
words.add(<word-in-english>);
words.add(<word-in-chinese>);
words.add(<word-in-russian>);
words.add(<word-in-arabic>);
What's the best way to put these into my Java code? Is there a way to do it other than using "\u" escape for every character in a string? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将编辑器/IDE的编码设置为UTF-8,也可以将java编译器的编码设置为UTF-8。对于国际项目来说,这开始越来越成为一种惯例。
不幸的是,您需要将 IDE 字体设置为完整的 unicode 字体,可能为 35 MB 左右。或者使用
native2ascii
来转义缺少的中文“\uXXXX”。根据您的来源,您可能会使用每种语言的文件。
You can set the encoding of editor/IDE to UTF-8, and java compiler too. For international projects this begins to become more and more a convention.
Unfortunately you would need to set your IDE font to a full unicode font which might be 35 MB or such. Or use for a missing chinese "\uXXXX" escaping, using
native2ascii
.Depending on your sources, you might use files per language.
为了使其工作,您必须执行以下两件事:
以 Unicode 格式 (UTF-8) 保存源文件。如何执行此操作取决于 IDE/文本编辑器。
通过指定 UTF-8 字符集编译文件。像这样:
javac -encoding utf-8 MyFile.java
In order for it to work you must do these 2 things:
Save the source file in Unicode format (UTF-8). How to do this is IDE/Text Editor dependent.
Compile the file by specifying the UTF-8 charset. Like this:
javac -encoding utf-8 MyFile.java
据我所知,将任何 Unicode 字符放入 Java 代码(包括 RTL 语言)中都没有问题。这在一定程度上取决于您的 IDE,但我相信所有现代 IDE 都支持 RTL 类型。至少 Eclipse 是这样。
您必须使用
UTF-8
字符集保存源代码。同样,这取决于您的 IDE。我在 Eclipse 上右键单击文件,然后选择资源并将其编码更改为 UTF-8。有时在 IDE 中输入 RTL 文本并不方便。在这种情况下,使用其他程序(MS Word、记事本等)键入文本,然后将其复制并粘贴到 java 代码中。
顺便说一句,考虑将 unicode 字符串存储在单独的资源文件中。通常更方便。
As far as I know there is no problem to put any Unicode characters into your java code including RTL languages. It a little bit depends on your IDE but I believe that all modern IDEs support RTL typing. At least Eclipse does.
You have to save your source code using
UTF-8
charset. Again it depends on your IDE. I eclipse right-click on file, then choose resource and change its encoding to UTF-8.Sometimes it is just not convenient to type RTL texts in IDE. In this case type text using other program (MS Word, Notepad etc) and then copy and paste it into java code.
BTW think about storing unicode strings in separate resouce file. It is usually more convenient.
不应该这样工作:
注意 UTF-16 。
shouldn't something like this work:
Pay attention to UTF-16 .