Question about the String.replaceAll() and String.replaceFirst() method
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"><开发者_如何学C;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:
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.
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.
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.
Here's an example of replaceAll
and replaceFirst
usage:
String s = "Hi there! John here!";
s = s.replaceAll("ere", "arrr");
System.out.println(s);
// prints "Hi tharrr! John harrr!"
s = s.replaceFirst("!", "?");
System.out.println(s);
// prints "Hi tharrr? John harrr!"
As others have mentioned, both are actually regex-based:
System.out.println("Hello???".replaceAll("?", "!"));
// throws PatternSyntaxException: Dangling meta character '?'
The problem here is that ?
is a regex metacharacter, and to treat it as a literal question mark, you have to escape it:
System.out.println("Hello???".replaceAll("\\?", "!"));
// prints "Hello!!!"
Sometimes the string the pattern is an unknown, in which case you can use Pattern.quote
:
String unknown = "?";
System.out.println(
"Hello???".replaceAll(Pattern.quote(unknown), "!")
); // prints "Hello!!!"
To complicate matters slightly, the replacement string is actually also regex-based.
System.out.println(
"USD 10".replaceAll("USD", "dollar$")
);
// throws StringIndexOutOfBoundsException: index out of range: 7
To quote a replacement string, you use Matcher.quoteReplacement
:
System.out.println(
"USD 10".replaceAll("USD", Matcher.quoteReplacement("dollar$"))
); // prints "dollar$ 10"
But what if I just want to do a simple no-regex replacement?
Then, to use replaceFirst
, you have to quote both the pattern and the replacement strings:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
String replaceFrom = ...;
String replaceTo = ...;
String before = ...;
String after = before.replaceFirst(
Pattern.quote(replaceFrom), Matcher.quoteReplacement(replaceTo)
);
And if you need a replaceAll
, then you just use the usual non-regex replace
method instead:
String after = before.replace(replaceFrom, replaceTo);
精彩评论