views:

308

answers:

4

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.

  1. In the original string I got, I can replace the string such as <div class="more"> to something else.
  2. 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&amp;amp;ps=20&amp;amp;since=1273518953218&amp;amp;sinceEntryId=abc-def-123-456"&gt;More...&lt;/a&gt;&lt;/div&gt;

I tried these two methods:

originalString.replaceFirst(moreTag, newContent);
originalString.replaceAll(moreTag, newContent);

Thanks in advance.

+2  A: 

You need to get hold of the result of the replacement and use it further:

String newString = originalString.replaceFirst(moreTag, newContent);
System.out.println(newString);

Explanation: strings in Java are immutable. The behavioral methods of java.lang.String won't change the internal value. They instead will return the modified result.

If that still doesn't return the desired result, then the moreTag simply didn't match anything. The methods you mentioned expects a regular expression. You can find in the Pattern javadoc how to compose a valid regex pattern.

BalusC
hello,allThanks for the quick responses. I defined the moreTag like this where I considered the escape character, and replaceAll() works in this case.String moreTag = new String("<div class=\"more\">");However, when I construct the longer string such as the one showed in my original question, I did not do anything about the double quotes or http://I got the string like this:String removedMoreTag=finalContent.substring(moreStartIndex, moreSecondIndex+6);Is this the problem? Should I do something about the double quotes in this removedMoreTag before making the replaceAll() call?
Java Doe
[You shouldn't parse HTML with regex](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454). You need to parse HTML with a HTML parser. I recommend [JSoup](http://jsoup.org/) for this.
BalusC
I got it work now. Thanks so much, everyone! :)
Java Doe
Hello, I am back again. I was able to find the solution using Java 1.5 API (String myFinalContent = finalContent.replaceFirst(Pattern.quote(string1), Matcher.quoteReplacement(string2));). Now, I just found out that in the codestream I am building, I can only use Java 1.4 API. Could somebody advice me how I can easily convert a string to REGULAR EXPRESSION? I did lots of research last night but could not find an answer. Really appreciated....
Java Doe
@Java Doe: You just need to **manually** quote the pattern and the replacement. Do `System.out.println()` of them in Java 1.5 to learn how they should actually look like and use that instead. Or look/copy the source of those Java 1.5 methods.
BalusC
A: 

Can you show us your code ?

Are you aware that your moreTag string (the first argument to replaceFirst/replaceAll) is assumed to be a regular expression ?

If you dont want regular expressions here, you can use the plain replace method.

leonbloy
A: 

Strings are immutable, do you save the result of replace operation?

Test you regular expression if it really matches what do you think, you'll find some tools for that online. Especially take care of proper escaping of special characters.

Also, if you do not need regular expressions, consider using StringTokenizer and StringBuilder.

binary_runner
A: 
polygenelubricants