private static char[] replaceSpaceInString(char[] str, int length) {
int spaceCounter = 0;
//First lets calculate number of spaces
for (int i = 0; i < length; i++) {
if (str[i] == ' ') {
spaceCounter++;
}
}
//calculate new size
int newLength = length + 2 * spaceCounter;
char[] newArray = new char[newLength + 1];
newArray[newLength] = '\0';
int newArrayPosition = 0;
for (int i = 0; i < length; i++) {
if (str[i] == ' ') {
newArray[newArrayPosition] = '%';
newArray[newArrayPosition + 1] = '2';
newArray[newArrayPosition + 2] = '0';
newArrayPosition = newArrayPosition + 3;
} else {
newArray[newArrayPosition] = str[i];
newArrayPosition++;
}
}
return newArray;
}
refer: http://stackoverflow.com/questions/10007631/write-a-method-to-replace-all-spaces-in-a-string-with-20