I have a method that recurivley removed leading character of a passed string that are unwanted. if a character at index 0 in the string is unwanted then I call the method again removing that index 0 untill a character that is wanted is found, then I want to return that string, however the string that is being returned is the very original string. I guess I am not understanding recursion properly?
Code:
private String chop(String string){
if ( !( doesCharacterExist(string.charAt(0)) ) ){
string = string.substring(1,string.length());
chop(string);
}
return string;
}
If I do a print out on each iteration I see the string get smaller however the return is the original string, If I put an else{ return string } I get the same behavior to. I figured by renaming string I would eliminate the original string's memory, guess not.
Any suggestions?
THanks