你能建议我在哪里犯错:
public static char[] replaceSpaces(String string){
int length = string.length();
int spaceCount = 0;
for(int i =0; i<=length-1;i++){if(string.charAt(i)==' '){spaceCount++;}}
int index = length + spaceCount*2;
char[] charString = new char[index];
int k=0;
for(int i=0;i<index-1;i++){
if(string.charAt(k) == ' '){
charString[i] = '%';
charString[i+1] = '2';
charString[i+2] = '0';
i = i +2;
k=k+1;}
else
charString[i] = string.charAt(k);
k++;
}
return charString;}
}
它输出wa%20cf而我想要“wa%20dcf”作为输出。基本上我用“%20”替换每个空格。此外,此代码不适用于两个或多个空格。