java字符串到char数组识别空格

时间:2016-06-22 17:25:53

标签: java arrays string

我正在尝试编写一些java,其中一个字符串被传递到一个char数组,在我得到该位工作正常的那一刻,但是我现在需要增加功能,以便数组能够识别空格在原始String中并将它们输出到数组中或将它们更改为“ - ”,以便代码的输出将显示空格。

private static char[] checkForJ(String encodeInput){
        StringBuilder tempString = new StringBuilder();

        encodeInput = encodeInput.toLowerCase();
        char[] ch  = encodeInput.toCharArray();
        for(char x : ch){
            if(x == ' '){

                x = '-';
                tempString.append(x);
            }
            if(x == 'j'){
                x = 'i';
                tempString.append(x);
            }else {
                tempString.append(x);
            }
        }
        String finalString = tempString.toString();
        char[] newChar = finalString.toCharArray();
        return newChar;

String从外部传递到方法中,然后输出数组以进行更多处理。

输入:abc def ghi jkl

预期输出:abc-def-ghi-ikl

实际输出:abcdefghiikl

1 个答案:

答案 0 :(得分:0)

稍加修改即可:在第二个SELECT TAG, ZST, sum(R1H00 + R1H01 + R1H02 + R1H03 + R1H04 + R1H05 + R1H06 + R1H07 + R1H08 + R1H09 + R1H10 + R1H11 + R1H12 + R1H13 + R1H14 + R1H15 + R1H16 + R1H17 + R1H18 + R1H19 + R1H20 + R1H21 + R1H22 + R1H23) AS TOTAL FROM Klassendaten WHERE KL = "SWISS7_PW" GROUP BY TAG, ZST; 之前添加else

if

然而,这非常罗嗦。它可以一次完成:

private static char[] checkForJ(String encodeInput){
    StringBuilder tempString = new StringBuilder();
    encodeInput = encodeInput.toLowerCase();
    char[] ch  = encodeInput.toCharArray();
    for(char x : ch){
        if(x == ' '){
            x = '-';
            tempString.append(x);
        }else if(x == 'j'){
            x = 'i';
            tempString.append(x);
        }else {
            tempString.append(x);
        }
    }
    String finalString = tempString.toString();
    char[] newChar = finalString.toCharArray();
    return newChar;
}

Ideone Demo