相关疑难解决方法(0)

形成字符串的字符串联会产生不同的结果

为什么,当我使用下面的操作来计算字符时,它是否返回数字而不是字符?它不应该给出相同的结果吗?

ret += ... ; // returns numbers

ret = ret + ...; // returns chars
Run Code Online (Sandbox Code Playgroud)

下面的代码重复了字符:

doubleChar("The")→"TThhee"

public String doubleChar(String str) {

    String ret = "";
    for(int i = 0; i < str.length(); i++) {
        ret = ret + str.charAt(i) + str.charAt(i); // it concatenates the letters correctly
        //ret += str.charAt(i) + str.charAt(i); // it concatenates numbers
    }
    return ret;
}
Run Code Online (Sandbox Code Playgroud)

java string concatenation char

10
推荐指数
1
解决办法
1838
查看次数

标签 统计

char ×1

concatenation ×1

java ×1

string ×1