Java相当于Python repr()? [英] Java equivalent of Python repr()?

查看:149
本文介绍了Java相当于Python repr()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种类似Python的repr的Java方法?例如,假设该函数名为repr,

Is there a Java method that works like Python's repr? For example, assuming the function were named repr,

"foo\n\tbar".repr()

将返回

"foo\n\tbar"

foo
        bar

as toString。

as toString does.

推荐答案

在某些项目中,我使用以下帮助函数来完成类似于Python的 repr 的字符串:

In some projects, I use the following helper function to accomplish something akin to Python's repr for strings:

private static final char CONTROL_LIMIT = ' ';
private static final char PRINTABLE_LIMIT = '\u007e';
private static final char[] HEX_DIGITS = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

public static String toPrintableRepresentation(String source) {

    if( source == null ) return null;
    else {

        final StringBuilder sb = new StringBuilder();
        final int limit = source.length();
        char[] hexbuf = null;

        int pointer = 0;

        sb.append('"');

        while( pointer < limit ) {

            int ch = source.charAt(pointer++);

            switch( ch ) {

            case '\0': sb.append("\\0"); break;
            case '\t': sb.append("\\t"); break;
            case '\n': sb.append("\\n"); break;
            case '\r': sb.append("\\r"); break;
            case '\"': sb.append("\\\""); break;
            case '\\': sb.append("\\\\"); break;

            default:
                if( CONTROL_LIMIT <= ch && ch <= PRINTABLE_LIMIT ) sb.append((char)ch);
                else {

                    sb.append("\\u");

                    if( hexbuf == null ) 
                        hexbuf = new char[4];

                    for( int offs = 4; offs > 0; ) {

                        hexbuf[--offs] = HEX_DIGITS[ch & 0xf];
                        ch >>>= 4; 
                    }

                    sb.append(hexbuf, 0, 4);
                }
            }
        }

        return sb.append('"').toString();
    }
}

这里给出的许多其他解决方案的主要优点是,它不会仅过滤一组有限的非 - 可打印的字符(比如替换基于的解决方案),但只是所有不可打印的ASCII字符。其中一些可能写得更好,但实际上它的工作......

Its main advantage over many of the other solutions given here is, that it does not filter only a limited set of non-printable characters (like those replace-based solutions), but simply all non-printable ASCII characters. Some of it could have been written slightly nicer, but it actually does its job...

注意,就像Python函数一样,这个函数会用引号括起字符串。如果你不想这样,你将不得不消除 append('' while 循环之前和之后的调用。

Note, that like the Python function, this one will surround the string with quotes. If you do not want that, you will have to eliminate the append('"') calls before and after the while loop.

这篇关于Java相当于Python repr()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆