As far as I know, in C we can modify a char-array in-place and then append the \0 to make the char-array shorter. I am wondering how that's done in Java.
Assume we want to remove the spaces from a char-array in Java.
char[] str = new String("cat love dogs").toCharArray();
for(int i = 0; i < str.length; i++) {
if(str[i] == ' ')
str[i] = '';
}
This does not seem to work.
EDIT: I already know about built-in functions to replace some chars with another in a string. I am not asking that. Curious to modify a char-array in-place with O(1) extra space.