1

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);
}}
Welt
  • 41
  • 7
  • 4
    Does this answer your question? [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Johannes Kuhn Jan 30 '22 at 02:47

1 Answers1

2

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.

Richard K Yu
  • 2,152
  • 3
  • 8
  • 21
  • I didnt downote, but is `string1.contains(string2)` required? – Aditya Arora Jan 30 '22 at 03:17
  • Does this give the same result as `replaceAll`? – Aditya Arora Jan 30 '22 at 03:19
  • @AdityaArora It doesn't, the difference is replaceAll removes all instances of matches from the original word while this will remove matches that aren't from the original word but formed by intermediate steps as well (put in a last paragraph to clarify). Not sure what behavior the OP wants though when he says "one at a time." – Richard K Yu Jan 30 '22 at 03:22
  • @RichardKYu I meant it exactly how you answered it. Thank you so much. – Welt Jan 30 '22 at 04:46