我想在使用 Eclipse 进行 Java 开发时在断点处查看字符串变量的值。
字符串变量显示为[47, 110, 109, 107, 111]
,这是有道理的,因为字符串实际上是 ASCII 字符的数组。
但是,我不想每次检查字符串时都将 ASCII 值转换为字符。如何让调试器显示字符串而不是 ASCII 值数组?
最佳答案
尝试通过创建字符串对象的新实例,将数组转换为具有正确编码的字符串
:
String s = new String(bytes,"ASCII");
System.out.println(s);
这是一个简短的示例来演示:
String example = "This is an example";
byte[] bytes = example.getBytes();
System.out.println("Text : " + example);
System.out.println("Text [Byte Format] : " + bytes);
System.out.println("Text [Byte Format] : " + bytes.toString());
String s = null;
try {
s = new String(bytes, "ASCII");
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("Text from bytes: " + s);
输出:
Text : This is an example
Text [Byte Format] : [B@187aeca
Text [Byte Format] : [B@187aeca
Text from bytes: This is an example
引用文献:
关于java - 如何在 Eclipse 调试器中查看 Java 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12060717/