I need to do a simple string replace operation on a segment of string. I ran into the following issue and hope to get some advice.
- In the original string I got, I can replace the string such as
<div class="more">to something else. - BUT, in the same original string, if I want to replace a much long string such as the following, it won’t work. Nothing gets replaced after the call.
<div class="more"><a href="http://SERVER_name/profiles/atom/mv/theboard/entries/related.do?email=xyz.com&ps=20&since=1273518953218&sinceEntryId=abc-def-123-456">More...</a></div>
I tried these two methods:
originalString.replaceFirst(moreTag, newContent);
originalString.replaceAll(moreTag, newContent);
Thanks in advance.
You need to get hold of the result of the replacement and use it further:
Explanation: strings in Java are immutable. The behavioral methods of
java.lang.Stringwon’t change the internal value. They instead will return the modified result.If that still doesn’t return the desired result, then the
moreTagsimply didn’t match anything. The methods you mentioned expects a regular expression. You can find in thePatternjavadoc how to compose a valid regex pattern.