且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Java / Eclipse中使用特殊字符串

更新时间:2023-09-29 13:35:28

问题是,您使用的字符无法在编码中文件设置为(Cp1252)。我看到的方式基本上有两个选择:



选项1. 更改编码 根据IBM ,你应将编码设置为UTF-8。我相信这会解决你的问题。



  • 将全局文本文件编码首选项Workbench> Editors设置为UTF -8。

  • 如果需要UTF-8以外的编码,请在单个文件上设置编码,而不是使用全局首选项设置。要执行此操作,请使用文件>属性>信息菜单选项来设置单个文件上的编码。


选项2. 删除Cp1252字符编码不支持的字符。您可以使用 Unicode转义序列(\uxxxx)。虽然这将允许您保存文件,但并不一定是***解决方案。



对于您在此问题中指定的字符是Unicode转义序列: p>

 ♥\\\♥ 
♦\\\♦
♣\\\♣
♠\\\♠


How can I use/display characters like ♥, ♦, ♣, or ♠ in Java/Eclipse?

When I try to use them directly, e.g. in the source code, Eclipse cannot save the file.

What can I do?

Edit: How can I find the unicode escape sequence?

The problem is that the characters you are using cannot be represented in the encoding you have the file set to (Cp1252). The way I see it, you essentially have two options:

Option 1. Change the encoding. According to IBM, you should set the encoding to UTF-8. I believe this would solve your problem.

  • Set the global text file encoding preference Workbench > Editors to "UTF-8".
  • If an encoding other than UTF-8 is required, set the encoding on the individual file rather than using the global preference setting. To do this use the File > Properties > Info menu selection to set the encoding on an individual file.

Option 2. Remove the characters which are not supported by the "Cp1252" character encoding. You can replace the unsupported characters with Unicode escape sequences (\uxxxx). While this would allow you to save your file, it is not necessarily the best solution.

For the characters you specified in your question here are the Unicode escape sequences:

♥ \u2665
♦ \u2666
♣ \u2663
♠ \u2660