在toString()中获取垃圾而不是字符串

时间:2013-05-29 04:51:52

标签: java tostring

我有一个小方法。它是带有eng字符的字符串并返回rus字符串(音译);但这有点不对劲。我不知道是什么。它返回的不是俄语字符串,而是一些垃圾,如“[C @ 4057db80”;

public String getRussianSting(String engString) {
    char[] engCharString = engString.toLowerCase().toCharArray();
    char[] rusCharString = new char[30];

    for (int i = 0; i <= engCharString.length - 1; i++) {
        if (engCharString[i] == ' ')
            continue;

        if (i + 1 <= engCharString.length - 1) {
            if (engCharString[i] == 'c' && engCharString[i + 1] == 'h') {
                rusCharString[i] = 'ч';
                i++;
                continue;

            } else if (engCharString[i] == 's' && engCharString[i + 1] == 'h') {
                rusCharString[i] = 'ш';
                i++;
                continue;

            } else if (engCharString[i] == 't' && engCharString[i + 1] == 'z') {
                rusCharString[i] = 'ц';
                i++;
                continue;

            } else if (engCharString[i] == 'y' && engCharString[i + 1] == 'i') {
                rusCharString[i] = 'ы';
                i++;

            } else if (engCharString[i] == 'y' && engCharString[i + 1] == 'e') {
                rusCharString[i] = 'э';
                i++;
                continue;

            } else if (engCharString[i] == 'y' && engCharString[i + 1] == 'u') {
                rusCharString[i] = 'ю';
                i++;
                continue;

            } else if (engCharString[i] == 'y' && engCharString[i + 1] == 'a') {
                rusCharString[i] = 'я';
                i++;
                continue;
            }
        }

        switch (engCharString[i]) {
            case 'a':
                rusCharString[i] = 'а';
                break;
            case 'b':
                rusCharString[i] = 'б';
                break;
            case 'v':
                rusCharString[i] = 'в';
                break;
            case 'g':
                rusCharString[i] = 'г';
                break;
            case 'd':
                rusCharString[i] = 'д';
                break;
            case 'e':
                rusCharString[i] = 'е';
                break;
            case 'j':
                rusCharString[i] = 'ж';
                break;
            case 'z':
                rusCharString[i] = 'з';
                break;
            case 'i':
                rusCharString[i] = 'и';
                break;
            case 'k':
                rusCharString[i] = 'к';
                break;
            case 'l':
                rusCharString[i] = 'л';
                break;
            case 'm':
                rusCharString[i] = 'м';
                break;
            case 'n':
                rusCharString[i] = 'н';
                break;
            case 'o':
                rusCharString[i] = 'о';
                break;
            case 'p':
                rusCharString[i] = 'п';
                break;
            case 'r':
                rusCharString[i] = 'р';
                break;
            case 's':
                rusCharString[i] = 'с';
                break;
            case 't':
                rusCharString[i] = 'т';
                break;
            case 'u':
                rusCharString[i] = 'у';
                break;
            case 'f':
                rusCharString[i] = 'ф';
                break;
            case 'h':
                rusCharString[i] = 'х';
                break;
            case '\'':
                rusCharString[i] = 'ь';
                break;
            default:
                break;
        }
    }
    return rusCharString.toString();
}

2 个答案:

答案 0 :(得分:3)

return rusCharString.toString();

将返回显示数组的对象表示的String。这是char[]用于敏感数据而不是String的一个主要原因。

<强>尝试:

return new String(rusCharString);

答案 1 :(得分:1)

问题在于,当您使用数组的toString时,它将使用它从Object类继承的数组。

如果您想获得正确的toString数组,请使用java.util.Arrays.toString(rusCharString)。您看到的垃圾是该数组的哈希码。

如果要单独打印哈希码,请使用rusCharArray.hashCode()