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);
}