使用StringBuilder从提取的字符中构建字符串

时间:2016-04-20 01:14:42

标签: java android

我无法理解为什么我试图用这个for循环构建的字符串只返回一个字符。我有一个4字符的字符串,我迭代所有匹配'0'的字符,但逻辑只发生一次。我错过了什么?

private void updateDurationColor(SpinClassMovement movement){

        String duration = (String) TextFormatUtil.getFormattedTimeInMinutesAndSeconds(movement.getMovementLengthInMinutes() + movement.getMovementLengthInSeconds());

        for(int i = 0; i < duration.length(); i++){

            if (duration.charAt(i) == '0'){

                Character zero = duration.charAt(i);
                StringBuilder colorDuration = new StringBuilder(zero);
                colorDuration.append(zero);

                setColor(mTimeRemaining,duration,colorDuration,Color.GRAY);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

我认为这是因为你正在循环中初始化“colorDuration”。试试这个。

private void updateDurationColor(SpinClassMovement movement){

        String duration = (String) TextFormatUtil.getFormattedTimeInMinutesAndSeconds(movement.getMovementLengthInMinutes() + movement.getMovementLengthInSeconds());
        StringBuilder colorDuration = new StringBuilder();

        for(int i = 0; i < duration.length(); i++){

            if (duration.charAt(i) == '0'){

                Character zero = duration.charAt(i);
                colorDuration.append(zero);

                setColor(mTimeRemaining,duration,colorDuration,Color.GRAY);
            }
        }
    }