【发布时间】:2014-01-27 17:48:26
【问题描述】:
为什么,当我使用下面的操作对字符求和时,它返回的是数字而不是字符?它不应该给出相同的结果吗?
ret += ... ; // returns numbers
ret = ret + ...; // returns chars
下面的代码复制了字符:
doubleChar("The") → "THhee"
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;
}
【问题讨论】:
标签: java string char concatenation