There is a task in which it is required to remove all substrings (remove) from a single string (base) without regard to case. The task was solved as follows:

public String withoutString(String base, String remove) { String result = ""; for (int i = 0; i < (base.length()-remove.length()+1); i++) if (!base.substring(i, i+remove.length()).toLowerCase().equals(remove.toLowerCase())) result += base.substring(i, i+1); else if (i+remove.length() < base.length()) i += remove.length()-1; else return result; for (int j = base.length()-remove.length()+1; j < base.length(); j++) result += base.substring(j, j+1); return result; } 

Without construction

 else return result; 

the program passes all checks, except in the case of "Hi HoHo", "Ho" (output: "Hi o").

Question : why is the return needed here (the second one, in fact), and the same break statement will not work here?

Thank you.

    2 answers 2

    Because you have in the loop:

     for (int j = base.length()-remove.length()+1; j < base.length(); j++) result += base.substring(j, j+1); 

    an unnecessary addition to the result string of the last unnecessary letter o , and adding:

     else return result; 

    you immediately exit the function without reaching the last letter.


    Your algorithm:

    1. If the i th index line does not equal the second, then everything is OK, we run further
    2. If it is equal, then skip this section so that it will not be written to result , provided that we have not reached the end of the line.
    3. If it is equal, and at the same time after that we already reach the end of the line after another jumping over, then we exit (logically)
    4. Write the last characters that the cycle jumped due to the condition

    That's right, so if you remove the 3rd step, you will add unnecessary characters.

    • I agree, but then other checks will not be passed. Actually, the problem itself: goo.gl/CrnF9q - Dmitry08
    • @ Dmitry08 corrected the answer, look - Denis
    • Yes, it seems to have completely figured it out. Thank you. - Dmitry08

    And you put the brackets on (very good practice, castati, especially in such cases) and it will become clearer:

     for (int i = 0; i < (base.length()-remove.length()+1); i++) { if (!base.substring(i, i+remove.length()).toLowerCase().equals(remove.toLowerCase())) { result += base.substring(i, i+1); } else if (i+remove.length() < base.length()) { i += remove.length()-1; } else { /* куда выкинет break в этом месте? */ return result; } } /* правильно, вот в эту точку, и будут совершаться лишние действия */ for (int j = base.length()-remove.length()+1; j < base.length(); j++) { result += base.substring(j, j+1); } return result; 
    • Thank you, I agree about break , but I still do not understand why the second return is still? - Dmitry08
    • @ Dmitry08, if you agree about break , it automatically means that you understood why return is instead ... - PinkTux
    • @ Dmitry08, in fact, return is needed here only because you need to return the String - in order for this to stop. return returns a function of the specified type, and break or continue work differently and only with a specific loop or nesting level. - And
    • @PinkTux @And Yes, I fully agree with the role of the second return . Thank you. - Dmitry08