[Logo]
Forums Register Login
replace() method for class StringBuilder
Hi,
it's not so clear to me the role of the second argument in replace() method defined in StringBuilder class. To be more precise I refer to the example showed on Mala Gupta "OCA Java SE 7" 2013 at page 196.

Here is the relevant code:


What I suppose, is that the second argument is the index of the first position of the array not to be 'modified', or the last one to be modified. However I'm wrong, in fact the code compile and run. Furthermore the code still compile and run the same way if I modify the second argument (ie by writing 3 or 6).

Can anyone explain me what the second argument stands for then?

Thank you, really, in advance.
(2 likes, 1 cow)
Hi Pagano,

According to the Java SE8 documentation for replace(), the second int argument refers to "the ending index, exclusive"
Also note that according to the same reference, "First the characters in the substring are removed and then the specified String is inserted at start."

My interpretation is that the second int argument represents the index position (in the original sequence), from which no further characters are deleted during the replace operation.
Basically, it will specify how many characters need to be removed from the original sequence before we start inserting the new String.

Some examples may make it more clearer (Disclaimer : I haven't tested these locally)







From the above examples, we see that the end index was needed to specify how many characters needed to be removed from the original StringBuilder sequence.

Hope this helps

Thanks,
Shantanu Bhate.
Hi, Shantanu Bhate
your explanation is just great; I do thank you then; really!

pagano monello
(1 like)
Shantanu provided indeed a great explanation. Have a cow for such a quality post!

I have some (very) minor remarks:
1/ Based on the study guide used, the OP is studying for the OCAJP 7 exam. So the Java 7 documentation of StringBuilder.replace() should be referred to (although I don't expect any difference between both explanations).
2/ In the code snippets you have introduced a few compiler errors, because sb is not defined (probably because you didn't have tested them locally)

And a few additions as well.

Shantanu Bhate wrote:My interpretation is that the second int argument represents the index position (in the original sequence), from which no further characters are deleted during the replace operation.
Basically, it will specify how many characters need to be removed from the original sequence before we start inserting the new String.

You are spot-on! If you subtract the startIndex from the endIndex, you'll know exactly how many characters will be replaced. So in this case: 4 - 2 = 2. That's the little aid I'm using myself when I'm unsure about the number of characters

Shantanu Bhate wrote:According to the Java SE8 documentation for replace(), the second int argument refers to "the ending index, exclusive"

Finally, I like to add that the exclusive ending index is very common in the Java API. Not only with characters (e.g. String.substring(), StringBuilder.replace(), StringBuilder.delete(), and so on), but also with elements in an array (e.g. Arrays.fill()) or list (e.g. ArrayList.subList()).

Hope it helps!
Kind regards,
Roel
Hi Roel,

Thank you for mentioning the things which I had missed in my post!
And thanks for the cow!

Regards,
Shantanu
Hi Roel,

I do thank you for your really punctual and useful observations. Thanks, really!

Kind regards,
pagano
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Dec 15, 2017 09:20:54.