java - 如何在 Eclipse 调试器中查看 Java 字符串?

标签 java eclipse

我想在使用 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/

相关文章:

java - 在 Java 中,当我们从正在执行 Future 线程的函数返回时会发生什么?

java - 如何使用 Spring Security 允许所有请求?

java - 为 Eclipse 编写新的重构插件?

java - 为什么在 Eclipse IDE 中不产生编译时异常除以零?

eclipse - 在编辑器中匹配关键字高亮颜色

java - fax4j : cannot send fax 的意外结果

java - java/spring 和 c++/qt 应用程序之间使用 websockets 进行通信

java - Android,无法使静态 getter setter 属性起作用吗?

android - 重复的 v4 支持库

java - 将 Eclipse 与 FindBugs 一起使用时,您能否将错误标记为不是错误并将其从错误列表中删除?