Remove Chars from String

Problem:

Remove Chars from String

Example: “yougetoffer” remove f and o, then return “yugeter”

Solution:

public String removeCharsFromString(String str){
    if(str == null || str.length() == 0) return null;
    int slow = 0;
    char[] array = str.toCharArray();
    for(int fast = 0; fast < array.length(); fast++){
        if(array[fast] != 'o' || array[fast] != 'f'){
            array[slow++] = array[fast];
        }
    }  
    return new String(array, 0, slow);  
}

Last updated

Was this helpful?