Home > OS >  Java recursive method that removes all instances of a second string in the first string and returns
Java recursive method that removes all instances of a second string in the first string and returns

Time:01-30

I am trying to write a recursive method in Java that accepts two strings and then goes ahead and removes the instances of the second string from the first string (one at a time).

ex. String 1 == Mississippi, String 2 iss first recursion == Missippi then the final result should return Mippi

public class RecursionEx {


public static void main(String[] args) {
    String str1 = "Mississippi";
    String str2 = "iss";
    
    System.out.println(repString(str1, str2));


}
public  static String repString(String string1, String string2) {
    //base case
    if(string1.length()== 0)
        return "";  
    //recursive case
    if (string1.substring(0, string2.length()) == string2)
        return repString(string1.substring(string2.length()), string2);
    else 
        return repString(string1.substring(1), string2);
}}

CodePudding user response:

Like the comment suggests, you should use equals() when comparing strings in Java, but you can also simplify your life by using the contains and removeFirst method for strings to deal with this recursive task.

I added a print line to the recursive function to show it is removing one instance of string2 at a time from string1.

public class StringRecursion {

    public static void main(String[] args) {
    String str1 = "Mississippi";
    String str2 = "iss";

    System.out.println(repString(str1, str2));

    }
    
    public static String repString(String string1, String string2) {
    if(string1.contains(string2)) {
        string1 = string1.replaceFirst(string2, "");
        System.out.println("The string is currently: "  string1);
    }
    else {
        return string1;
    }
    
    return repString(string1, string2);
    }

}

Output:

The string is currently: Missippi
The string is currently: Mippi
Mippi

Important: One other thing to consider with such an approach is if you want the pattern "iss" formed by an intermediate removal to also be removed. For instance, if you have the word "iissss" and want to remove "iss" it would become "" after running this even though iss does not appear twice in the word initially.

If you want to have the behavior mimic replaceAll function, where we are looking to only get rid of the "iss" patterns in the very first word and not the ones that appear in intermediate steps, I believe the function:

public static String repString(String string1, String string2) {
    if(string1.contains(string2)) {
        Pattern pattern = Pattern.compile(string2);
        long originalCounts = pattern.matcher(string1).results().count();
        
        string1 = string1.replaceFirst(string2, "");
        long newCounts = pattern.matcher(string1).results().count();
        
        if(originalCounts == newCounts) {
        Matcher matcher = pattern.matcher(string1);
        matcher.find();
        int startPosition = matcher.end();
        
        //Skip the generated matching pattern that appears in-between.
        return string1.substring(0, startPosition)   repString(string1.substring(startPosition), string2);
        }
        
        //System.out.println("The string is currently: "  string1);
    }
    else {
        return string1;
    }
    
    return repString(string1, string2);
}

will be sufficient instead.

  •  Tags:  
  • Related