Reputation: 20065
I often use the method replace(CharSequence,CharSequence)
in Java, but today I encountered something I don't really understand.
When I do :
"|||".replace("||","| |");
Why it results in : | ||
and not | | |
? Why the second |
can't be include in two patterns?
To resolve my problem I have to write "|||".replace("||","| |").replace("||","| |");
which looks a bit redundant.
Any one have an explanation to help me understand better the mechanism behind?
EDIT
So it has been said that it's the particular case specify in the javadoc, but it's wrong the example is the following :
* The replacement proceeds from the beginning of the string to the end, for
* example, replacing "aa" with "b" in the string "aaa" will result in
* "ba" rather than "ab".
So it's different from my case because the result "ba" is not a match for "aa" anymore. Whereas in my case the result "| ||" still contains the pattern.
Upvotes: 2
Views: 206
Reputation: 36611
Not really an answer on the why, but it is a good thing that
"|||".replace("||","| |");
does not result in "| | |"
. If that would be the result, it means the replacement is also performed on a part of the String where replacements already took place.
That would make it very easy to write an infinite loop, e.g. "|".replace("|", "||" );
would result in "||||||...."
until your String becomes so big you run out-of-memory
Upvotes: 1
Reputation: 647
What you are looking for is
"|||".replace("|","| ");
or
"|||".replace("|","| ").trim();
if you don't want the last space.
The why i let for you to self study. Tip: search for regular expression replacement.
Upvotes: 0
Reputation: 500407
There are two possibilities for why replace()
might behave the way you expect it to:
It could consider overlapping matches (in your example: match characters 1 & 2, and then 2 & 3). It does not do that.
It could substitute recursively, meaning that stuff that got added/changed as a result of a substitution would be eligible for further matches. It does not do this either.
Upvotes: 1
Reputation: 47608
The replace starts by looking at the charsequence to find in your original string and once found, it says, ok the match starts in start
and ends in end
, let's replace that part with the replacement and then keep on looking for the next match from the end + 1
position
Upvotes: 1
Reputation: 78650
There are two things going on here that could cause the confusion. First, replace
will not process the same character twice, so the middle |
can only apply to a single replace. Second, replace does not process the replacement characters, so after the first replace you will have | ||
, but the 2nd |
is part of the first replacement.
So basically what you get is after one replace, you have the first ||
is replaced and the remainder is |
which does not match so the replace is complete.
Upvotes: 3