There is a String text
System.out.println(text);
looks like this
So the traveller sat down by the side of that old man,
face to face with the serene sunset;
and all his friends came softly back and stood around him.
Another String subText
System.out.println(subText);
is simply a part of the above string and looks like this
So the traveller sat down by the side of that old man,
face to face with the serene sunset;
I need to get rid of this subText
part text = text.replaceAll(subtext, "");
but this does not do anything with the text?
In this particular case it does not matter, but you really should use replace
instead of replaceAll
:
text = text.replace(subtext, "");
The replaceAll
method uses regular expressions, and some characters have special meaning.
In this particular case you won't see any difference because there must be something subtly different in subtext
, so it cannot be found in text
. Perhaps there's extra white space or the line break character is encoded differently. It's very hard to see differences in white space looking at output produced by System.out.println
.