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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/200691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:16:10  来源:igfitidea点击:

How to use Special Chars in Java/Eclipse

javaeclipseunicodeencodingcp1252

提问by Burkhard

How can I use/display characters like ?, ?, ?, or ? in Java/Eclipse?

如何使用/显示 ?, ?, ?, or ?, ?, ?, or ? 在 Java/Eclipse 中?

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

当我尝试直接使用它们时,例如在源代码中,Eclipse 无法保存文件。

What can I do?

我能做什么?

Edit: How can I find the unicode escape sequence?

编辑:如何找到 unicode 转义序列?

采纳答案by Joe Lencioni

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:

问题是您使用的字符无法以您将文件设置为 (Cp1252) 的编码表示。在我看来,您基本上有两种选择:

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

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

  • 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.
  • 将全局文本文件编码首选项 Workbench > Editors 设置为“UTF-8”。
  • 如果需要 UTF-8 以外的编码,请在单个文件上设置编码,而不是使用全局首选项设置。为此,请使用文件 > 属性 > 信息菜单选项来设置单个文件的编码。

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.

选项 2.删除“Cp1252”字符编码不支持的字符。您可以使用Unicode 转义序列(\uxxxx)替换不受支持的字符。虽然这可以让您保存文件,但它不一定是最佳解决方案。

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

对于您在问题中指定的字符,这里是 Unicode 转义序列:

? \u2665
? \u2666
? \u2663
? \u2660

回答by Jon Skeet

Either change your encoding to one which will cope, e.g. UTF-8, or find the relevant Unicode number and use a \uxxxx escape sequence to represent it.

要么将您的编码更改为可以应对的编码,例如 UTF-8,要么找到相关的 Unicode 编号并使用 \uxxxx 转义序列来表示它。

回答by idrosid

It can be solved by setting encoding in eclipse:

可以通过在eclipse中设置编码来解决:

1st way:

第一种方式:

At the menu select File-->Propertiesand then at the "Text file encoding"section: Select Other radio, Select UTF-8 from combo -> Lastly click OK button

在菜单中选择文件--> 属性,然后在“文本文件编码”部分:选择其他单选,从组合中选择 UTF-8 -> 最后单击确定按钮

2nd way:

方式二:

Right click on specific file (say Test.java) -> Properties. In Text file encodingsection: Select Other radio, Select UTF-8 from combo -> Lastly click OK button

右键单击特定文件(例如 Test.java)-> Properties。在文本文件编码部分:选择其他收音机,从组合中选择 UTF-8 -> 最后单击确定按钮

3rd way:

第三种方式:

If you want to make this change for all your project go at Window-->Preferences--> General--> Workspace. In Text file encodingsection: Select Other radio, Select UTF-8 from combo -> Lastly click OK button

如果要对所有项目进行此更改,请转到Window-->Preferences--> General--> Workspace。在文本文件编码部分:选择其他收音机,从组合中选择 UTF-8 -> 最后单击确定按钮

回答by MSalters

Finding the unicode escape sequence: see these Unicode charts. Your characters are in the Misc. Symbols chart, \u2660 and up.

查找 Unicode 转义序列:请参阅这些Unicode 图表。你的角色在杂项中。符号图表,\u2660 及以上。

回答by sepehr

In Eclipse:

在日食中:

  1. Go to Window -> Preferences -> General -> Workspace -> TextFileEncoding
  2. Set it to UTF-8
  1. 转到窗口 -> 首选项 -> 常规 -> 工作区 -> TextFileEncoding
  2. 将其设置为 UTF-8

回答by Sonu

Windows Menu –> Preferences –> General (expand it) –> Workspace (click on it). Look for a box “Text File Encoding”. Default will be “Cp1252″. Change radio to select other and select “UTF-8″ from combo box.

Windows 菜单 –> 首选项 –> 常规(展开它)–> 工作区(单击它)。寻找“文本文件编码”框。默认值为“Cp1252”。更改单选以选择其他并从组合框中选择“UTF-8”。

回答by Piotr Findeisen

Expanding a bit on @Joe Lencioni answer

对@Joe Lencioni 的回答进行一些扩展

You can use AnyEdit Eclipse plugin (install-able from Eclipse marketplace) to easily convert Unicode text into Java Unicode escapes:

您可以使用 AnyEdit Eclipse 插件(可从 Eclipse 市场安装)轻松将 Unicode 文本转换为 Java Unicode 转义:

  • Select char/text with non-ASCII characters
  • right-click
  • Convert > To Unicode Notation
  • 选择带有非 ASCII 字符的字符/文本
  • 右键点击
  • 转换 > 到 Unicode 表示法

One minor caveat is that AnyEdit wants to save the file first which obviously is disallowed by Eclipse until you fix your text.

一个小警告是 AnyEdit 希望首先保存文件,这显然是 Eclipse 不允许的,直到您修复文本。