-1

I would like to share my code with you to help me improve.

I need to write a function that returns a String consisting of string1 minus all the characters of string2.

Here is what I tried, unfortunately it doesn't work that well:

public static String remove(String str1, String str2) {
    String empty = "";

    for (int i = 0; i < str2.length(); i++) { // hello ll
        for (int j = 0; j < str1.length(); j++) {
            if (str2.charAt(i) != str1.charAt(j)) {
                empty = empty + str1.charAt(j);

            }
        }
    }
    return empty;
}
Math4me
  • 153
  • 6
  • 1
    Does this answer your question? [Find difference between two Strings](https://stackoverflow.com/questions/12089967/find-difference-between-two-strings) – Amir Dora. Dec 05 '20 at 22:31

4 Answers4

1

You can use indexOf to check if a character does not exist in the second String. Currently, you are not checking all the characters of the second String before determining whether or not the current character should be added. Additionally, it is better to use a StringBuilder instead of concatenating inside a loop.

public static String remove(String str1, String str2) {
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < str1.length(); i++) {
        if (str2.indexOf(str1.charAt(i)) == -1) {
            sb.append(str1.charAt(i));
        }
    }
    return sb.toString();
}
Community
  • 1
  • 1
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

You can do it like:

public static String remove(String str1, String str2) {
    for (int i = 0; i < str2.length(); i++)
        str1 = str1.replace(str2.charAt(i) + "", "");
    return str1;
}

From the string 1 you replace all the character from string 2 by "".

Community
  • 1
  • 1
dreamcrash
  • 47,137
  • 25
  • 94
  • 117
0

In your solution, for each character in the first strings, you iterate over the characters in the second string, and append the character to the result if the two differ. This isn't the behavior that's expected - you need to append the character only if it differs from all the characters in the second string.

Keeping the format of the method you shared as much as possible, you'd want to do something like this:

public static String remove(String str1, String str2) {
    String empty = "";
    
    for (int i = 0; i < str2.length(); i++) {
        boolean found = false;
        for (int j = 0; j < str1.length() && !found; j++) {
            if (str2.charAt(i) == str1.charAt(j)) {
                found  = true;
            }
        }
        if (!found) {
            empty += str1.charAt(i)
    }
    return empty;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0

String consisting of string1 minus all the characters of string2:

public static void main(String[] args) {
    System.out.println(remove("hello", "ll")); // heo
    System.out.println(remove("hello", "eo")); // hll
    System.out.println(remove("hello", "lo")); // he
}
public static String remove(String str1, String str2) {
    return str1.codePoints()
            .filter(ch -> !str2.contains(Character.toString(ch)))
            .mapToObj(Character::toString)
            .collect(Collectors.joining());
}