I have a string
"apples, pears # and bananas\\ngrapes\\nbananas !apples"
I want to remove the string substring # and bananas.
private static String cleaned(String main, String substring) {
return main.replace(substring, "");
}
but replace method doesn't work somehow. I feel it has something to do with the # because if I try getting the index of # using main.indexOf("#") i get -1.
When I call the method directly like this
System.out.println(cleaned("apples, pears # and bananas\\ngrapes\\nbananas !apples", "# and bananas"));
it surprisingly works I get the correct answer
**apples, pears \ngrapes\nbananas !apples**. However when I call the method programmatically from another method like this
public static String stripComments(String text, String[] commentSymbols)
//some code
return cleaned(stringBuilder.toString(), textAfterComment.toString());
}
it does not produce the correct answer. I get **apples, pears and bananas\ngrapes\nbananas**
Why do I get -1 meaning that # is not found yet it is clearly in the string? And how can I remove the substring with or without the replace method ?