Remove certain chars in-place

Viewed 173

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.

4 Answers

well the best solution i can see is remove all spaces from your string first then get the chars from it :

 String myString = new String("cat love dogs").replaceAll("\\s+", ""); 
     // \\s+ is the regex for sapces 
    // myString becomes => catlovedogs
 char[] str = myString.toCharArray(); 

As String is immutable you may use a StringBuilder class:

StringBuilder sb = new StringBuilder("cat love dogs");

for(int i = 0; i < sb.length(); i++) {
        if(sb.charAt(i) == ' ') {
            sb.deleteCharAt(i);
            i--;
        }
    }

char[] str = sb.toString().toCharArray();

Now as far as space is concerned String class in Java is immutable!

Thus you cannot do anything in place there.

But you can use StringBuilder which is immutable.

Code:

StringBuilder stringBuilder = new StringBuilder("cat love dogs");
for(int i = 0; i < stringBuilder.length(); i++) {
    if (sb.charAt(i) == ' ')
        sb.deleteCharAt(i);
}
char[] str = stringBuilder.toString().toCharArray();

Or if you want to do it just using the char array itself its not possible as char arrays are not dynamic by themselves.

But here is what you could do:

char[] str = new String("cat love dogs").toCharArray();
int last = 0;
for(int i = 0; i < str.length; i++) {
    if(str[i] == ' '){
        str = ArrayUtils.remove(str, i);
    }
}
System.out.println(new String(str));
System.out.println(new String(str).length());

Output:

catlovedogs
11

For for this you need to import org.apache.commons.lang3.ArrayUtils

If you are not concerned about space:

In Java we can just set the character value to 0!

Here is the modified code:

char[] str = new String("cat love dogs").toCharArray();
for(int i = 0; i < str.length; i++) {
    if(str[i] == ' ')
        str[i] = 0;
}
System.out.println(new String(str));

Here is the output:

catlovedogs

I think the best way to do this is by creating a duplicate array of chars, where you copy every character besides the spaces and then return the new array. Here is how the code might look like:

        char[] str = new String("cat love dogs").toCharArray();
        List<Character> duplicate = new ArrayList<>();
        
        for(int i = 0; i < str.length; i++) {
            if(str[i] != ' ') {
                duplicate.add(str[i]);
            }
                
        } 

it's better to use ArrayList because you don't really know the final size of the array and this way you can go only as big as needed.

Related