• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on substring() method

 
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ""
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic