• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on substring() method  RSS feed

 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oracle documentation (http://docs.oracle.com/javase/7/docs/api/) for substring method gives a few examples for this method. I am puzzled by one of the examples.

"emptiness".substring(9) returns "" (an empty string)

The index value of 9 points to the NULL character at end of the string "emptiness". I get a runtime exception when i put this to test in Netbeans. How come this doc says an empty string "" is returned ? It should throw an exception in my opinion.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
N Sam wrote:The index value of 9 points to the NULL character at end of the string "emptiness".

There is no null character at the end of strings in Java. You may be thinking or C or C++.

It should throw an exception in my opinion.

On what do you base this opinion?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you mean this method.
That example is given to show that "emptiness".substring(9) was written so as not to throw an Exception. It says quite clearly that the index must not be larger than the size of the String, so you would get an Exception from "emptiness".substring(10)
 
N Sam
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that link is where the api documentation gives this example ("emptiness".substring(9)). When i execute this in IDE i do not get an exception (clearly the doc says that an exception will be thrown only if the index is greater than the length of string. In this case length = 9 and so the string has the last char at index = 8. Without throwing an exception, substring() returns "" (empty string, not null). Totally weird in my opinion because at index = 9, we are out of bounds of "emptiness". I found another discussion on this topic at this thread. But the explanation given there is not satisfactory. The documentation is correct, because it documents the exact behavior of this api (during execution). ie.... only index=10 throws an exception, but not index=9.


Never mind this post - i found the answer in another api i was reading. This is just a design characteristic of java strings. Following is from oracle doc.....
public int lastIndexOf(String str)
Returns the index within this string of the last occurrence of the specified substring. The last occurrence of the empty string "" is considered to occur at the index value this.length().
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It only helps obliquely. The empty string is a substring of every non‑null String, so it should be possible to extract an empty String from a substring method.

∴ The largest index for substring is l where l is the String's length, not l - 1.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of it this way

"emptiness".substring(0) gives you (the length of the string - 0) characters = 9 characters "emptiness"
"emptiness".substring(1) gives you (the length of the string - 1) characters = 8 characters "mptiness"
"emptiness".substring(2) gives you (the length of the string - 2) characters = 7 characters "ptiness"
"emptiness".substring(3) gives you (the length of the string - 3) characters = 6 characters "tiness"
"emptiness".substring(4) gives you (the length of the string - 4) characters = 5 characters "iness"
"emptiness".substring(5) gives you (the length of the string - 5) characters = 4 characters "ness"
"emptiness".substring(6) gives you (the length of the string - 6) characters = 3 characters "ess"
"emptiness".substring(7) gives you (the length of the string - 7) characters = 2 characters "ss"
"emptiness".substring(8) gives you (the length of the string - 8) characters = 1 characters "s"
"emptiness".substring(9) gives you (the length of the string - 9) characters = 0 characters ""
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yet another way to think about it is with respect to the corner/edge case where the string is the empty string "".

Probably makes for a more straightforward implementation, too.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!