Page 1 of 1

[HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Posted: Wed Jan 01, 2025 10:00 am
by raphaelzintec
Furthermore, any method that attempts to access an index that is beyond the range of this array throws IndexOutOfBoundsException.

so basically all String methods throw IndexOutOfBoundsException except substring() which throws StringIndexOutOfBoundsException ?

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Posted: Wed Jan 01, 2025 11:38 pm
by admin
For most of the methods (that throw IOOBE) of the String class, the JavaDoc say these methods throws IOOBE but the implementation of some of these methods (including substring) throw SIOOBE. Since SIOOBE extends IOOBE, the implementation is technically within the agreed contract.

The JavaDoc for indexOf method says it throws SIOOBE.

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Posted: Sun Jul 13, 2025 6:15 am
by dtruebin
21st Jun 2025 Build 2.20: the summary table at the beginning of the section mentions method firstIndexOf which exists neither in Java 17's nor in Java 21's String.

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Posted: Sun Jul 13, 2025 6:29 am
by dtruebin
21st Jun 2025 Build 2.20:

Code: Select all

//the following lines print false
System.out.println(s1.indent(0) == s1); //prints false even though there is no change
-- "there is no change" is clearly wrong, as demonstrated by the following JShell snippet:

Code: Select all

jshell> "a".stripIndent()
$86 ==> "a"
Even in the book itself, this is supported by this sentence a couple of paragraphs earlier:
More interestingly, if the last line is not terminated using a line terminator, a \n character is appended.

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Posted: Sun Jul 13, 2025 7:37 am
by admin
dtruebin wrote:
Sun Jul 13, 2025 6:15 am
21st Jun 2025 Build 2.20: the summary table at the beginning of the section mentions method firstIndexOf which exists neither in Java 17's nor in Java 21's String.
Correct. indexOf is actually returns the first index so there is no need for firstIndexOf. There is just lastIndexOf.

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Posted: Sun Jul 13, 2025 7:46 am
by admin
dtruebin wrote:
Sun Jul 13, 2025 6:29 am
21st Jun 2025 Build 2.20:

Code: Select all

//the following lines print false
System.out.println(s1.indent(0) == s1); //prints false even though there is no change
-- "there is no change" is clearly wrong, as demonstrated by the following JShell snippet:

Code: Select all

jshell> "a".stripIndent()
$86 ==> "a"
Even in the book itself, this is supported by this sentence a couple of paragraphs earlier:
More interestingly, if the last line is not terminated using a line terminator, a \n character is appended.
You are right.s1.indent(0) adds a new line character and so false is expected. Although "a".stripIndent() produces a different string with the same content.

Thank you for your feedback. Please let me know your name so that it can be added to the acknowledgements pages. You may email it to support@enthu... if you don't want to reveal it here.

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Posted: Mon Jul 14, 2025 3:35 am
by dtruebin
Thanks! It's Dmitrii Trubin.

Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]

Posted: Mon Jul 14, 2025 11:31 pm
by admin
Thank, build 2.21 is already out. Your name will be there in build 2.22.