1

I'm trying to create a method in JAVA for "subtracting" a substring from a given string. For example, if my input is "committee" and "meet" the output should be "comit".

This is what I have so far, but it's not doing what it is supposed to. I'm pretty sure the problem is with the iteration of the nested for loop at the end of the code, but I can't figure out what is the problem or how to fix it.

public static String remove(String str1, String str2) {
    char[] char1 = new char[str1.length()];
    char[] char2 = new char[str2.length()];
    char[] char3 = new char[str1.length()];
    int k = 0;

    for (int i = 0; i < str1.length(); i++) { // converts str1 to char1
        char1[i] = str1.charAt(i);
    }

    for (int j = 0; j < str2.length(); j++) { // converts str2 to char2
        char2[j] = str2.charAt(j);
    }

    for (int i = 0; i < char1.length; i++) { // loops through char1
        for (int j = 0; j < char2.length; j++) {
            if (char1[i] != char2[j]) {
                char3[k] = char1[i];
            }
        }
        k++;
    }
    return String.valueOf(char3);
}
Alexander
  • 2,925
  • 3
  • 33
  • 36
RONOLULU
  • 727
  • 1
  • 4
  • 12
  • 1
    The first two loops can be replaced by [`String.toCharArray`](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toCharArray()) – luk2302 Dec 05 '20 at 14:15
  • Your subtraction loop should remove the chars from `char2` that have already been subtracted. Otherwise one `m` in `char2` is enough to remove all `m`s in `char1`. – luk2302 Dec 05 '20 at 14:16
  • @luk2302 Should I place it inside the inner for loop? Also, which method could I use to do this? – RONOLULU Dec 05 '20 at 14:32

3 Answers3

0

While looping on the array of character that you wish to delete, you can use replaceFirst. The character on the original string that you have deleted should be tagged with a specific character so that you can revisit later when rebuilding the result.

public class Test1 {
    public static void main(String[] args) {
        String result = remove("committee", "meet");
        System.out.println(result);
    }

    //str1 is the original string, str2 is the
    //array of chars to be removed from str1
    public static String remove(String str1, String str2) {
        for (int i = 0; i < str2.length(); i++) {
            //tag the one deleted using specific character
            str1 = str1.replaceFirst(Character.toString(str2.charAt(i)), "#");
        } //end for

        StringBuilder sb = new StringBuilder();
        //Populate the non deleted chars
        for (int i = 0; i < str1.length(); i++) {
            //only copy character which has not yet been deleted
            if (str1.charAt(i) != '#') {
                sb.append(str1.charAt(i));
            } //end if
        } //end for
        return sb.toString();
    }
}

Sample Output:

comit
Community
  • 1
  • 1
Agus
  • 619
  • 5
  • 6
0

Wouldn't this be easier:

public class StringSubtract {
    public static void main(String[] args) {
        String result = subtract("committee", "meet");
        System.out.println(result);
    }

    public static String subtract(String str1, String str2) {
        for (int i = 0; i < str2.length(); i++) {
            str1 = str1.replaceFirst(Character.toString(str2.charAt(i)), "");
        } //end for

        return str1;
    }
}
TAM
  • 1,731
  • 13
  • 18
0

You can use StringBuilder for this purpose:

public static void main(String[] args) {
    System.out.println(remove("committee", "meet")); // comit
}
public static String remove(String str1, String str2) {
    // convert first string to StringBuilder
    StringBuilder sb = new StringBuilder(str1);
    // iterate over the characters of the second string
    for (char ch : str2.toCharArray()) {
        // index of the first occurrence of
        // this character in the first string
        int i = sb.indexOf(String.valueOf(ch));
        // if present
        if (i >= 0) {
            // remove this character
            sb.deleteCharAt(i);
        }
    }
    return sb.toString();
}

See also: Converting char[][] to char[]