I am confused by the official Javadoc which says
public StringBuffer replace(int start, int end, String str)
Replaces the characters in a substring of this sequence with characters in the specified String. The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. First the characters in the substring are removed and then the specified String is inserted at start. (This sequence will be lengthened to accommodate the specified String if necessary.)
What does "if no such character exists" mean ?
If I do the following
StringBuffer sb = new StringBuffer(" "); // 30 whitespace characters
sb.replace(3, 20, "123456789012345678901234567890");
I end up with the string " 123456789012345678901234567890 ".
I would have expected " 12345678901234567 " (length = 30), because I said to replace characters in the "source" StringBuffer from character 3 to 20 (20-3 = 17).
I have tried looking at OpenJDK implementation but I can't say it helped (I don't understand why System.arraycopy is called with srcpos = end, srcpos).