What's the difference between java.lang.String 's replace()
and replaceAll()
methods,
other than later uses regex? For simple substitutions like, replace .
with /
,
is there any difference?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Q: What's the difference between the
java.lang.String
methodsreplace()
andreplaceAll()
, other than that the later uses regex.A: Just the regex. They both replace all :)
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html
PS:
There's also a
replaceFirst()
(which takes a regex)The
replace()
method is overloaded to accept both a primitivechar
and aCharSequence
as arguments.Now as far as the performance is concerned, the
replace()
method is a bit faster thanreplaceAll()
because the latter first compiles the regex pattern and then matches before finally replacing whereas the former simply matches for the provided argument and replaces.Since we know the regex pattern matching is a bit more complex and consequently slower, then preferring
replace()
overreplaceAll()
is suggested whenever possible.For example, for simple substitutions like you mentioned, it is better to use:
instead of:
Note: the above conversion method arguments are system-dependent.
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
Replaces each substring of this string that matches the given regular expression with the given replacement.
As alluded to in wickeD's answer, with replaceAll the replacement string is handled differently between replace and replaceAll. I expected a[3] and a[4] to have the same value, but they are different.
The output of this is:
This is different from perl where the replacement does not require the extra level of escaping:
which prints \X 2
This can be quite a nuisance, as when trying to use the value returned by java.sql.DatabaseMetaData.getSearchStringEscape() with replaceAll().
replace
andreplaceAll
change thestring
and char in all the words butreplaceAll
supports regex (Regular Expression). There is alsoreplaceFirst
which is likereplaceAll
in that they both support regex and the both change strings and chars, the difference between them is that when you usereplaceFirst
with regex it replaces the first regex ONLY.And output:
There are two replace() method in Java, one of them takes character as first parameter and other takes CharSequence (which is super Interface for String, Stringbuffer, etc) as first parameter. Both these methods replaces all occurrences of char or CharSequence with the value you provide in 2nd parameter.
ReplaceAll method takes Regular expression as first parameter, so you need to give it some Regex and the matched content will be replaced by the String you pass in 2nd parameter.
For complete difference between replace()and replaceAll() method you can refer here Difference between replace(), replaceAll() and replaceFirst() method in Java String