[HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]
Moderator: admin
-
- Posts: 167
- Joined: Sun Apr 21, 2024 10:43 am
- Contact:
[HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]
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 ?
so basically all String methods throw IndexOutOfBoundsException except substring() which throws StringIndexOutOfBoundsException ?
-
- Site Admin
- Posts: 10394
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]
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.
The JavaDoc for indexOf method says it throws SIOOBE.
-
- Posts: 3
- Joined: Wed Jun 18, 2025 4:39 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]
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.
-
- Posts: 3
- Joined: Wed Jun 18, 2025 4:39 am
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]
21st Jun 2025 Build 2.20:
Even in the book itself, this is supported by this sentence a couple of paragraphs earlier:
-- "there is no change" is clearly wrong, as demonstrated by the following JShell snippet:Code: Select all
//the following lines print false System.out.println(s1.indent(0) == s1); //prints false even though there is no change
Code: Select all
jshell> "a".stripIndent()
$86 ==> "a"
More interestingly, if the last line is not terminated using a line terminator, a \n character is appended.
-
- Site Admin
- Posts: 10394
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]
Correct. indexOf is actually returns the first index so there is no need for firstIndexOf. There is just lastIndexOf.
-
- Site Admin
- Posts: 10394
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD-OCP17/21-Fundamentals Pg 265, Sec. 11.1.6 - manipulating-strings]
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.dtruebin wrote: ↑Sun Jul 13, 2025 6:29 am21st Jun 2025 Build 2.20:-- "there is no change" is clearly wrong, as demonstrated by the following JShell snippet:Code: Select all
//the following lines print false System.out.println(s1.indent(0) == s1); //prints false even though there is no change
Even in the book itself, this is supported by this sentence a couple of paragraphs earlier:Code: Select all
jshell> "a".stripIndent() $86 ==> "a"
More interestingly, if the last line is not terminated using a line terminator, a \n character is appended.
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.
Who is online
Users browsing this forum: No registered users and 284 guests