• 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

substring Vs charAt

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How substring and charAt methods interpret the stings??

For instance,
String s = "Java";
The length of the above sting is 4 (FROM 0 TO 3)
1. System.out.println(s.substring(4));
2. System.out.println(s.charAt(4));
The first one prints blank (no output) and the next one results in StringOutOfBoundsException.
s.substring(5) throws the above runtime exception.

What value is stored at position 4(after 'a') in the above string???

Thanks
Regards
Balaji

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You are seeing quite the expected behaviour. substring() with a single parameter starts at that particular index and extracts till the end of the string. (Remember, strings start at index 0)
So, when you say start from 4th position for a string which has index only till 3, what you get is an empty string.(If someone could clarify whether it is null or "" i would be happy.)
On the other hand, the parameter you have given for charAt is 4 which is larger than the length of the string, which causes the StringIndexOutOfBoundsException.
I believe that you have a background in C, since you are asking what is the last character.(In C it is \0 ). I guess there is no string terminator like that in Java. Hope someone can confirm.
------------------
Regards,
Shree
[This message has been edited by shree vijay (edited December 10, 2000).]
 
Balaji Sadasivam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply shree.
I personally feel that your reply is just another form of my question.
I would like to discuss your reply in two separate cases.
Case 1: (No String terminator)
-------
According to your statment, there is no string terminator in java. If this is true, both substring() and charAt() should result in RuntimeException. Unfortunately, that is not the case.
Case 2: (With String terminator)
-------
If string terminator is blank (or any other character for this matter), how come one method is showing output(assume it is blank) and the other results in exception FOR THE SAME STRING INDEX VALUE?
I guess something is sneaky here...

Balaji
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This is not a fulfilling reply i guess, but i checked the api and found that this difference is due to the definitions of substring() and charAt.
Check it out
The substring() method does indeed throw an exception in the following cases :
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
Unfortunately, it doesn't check the case if the beginindex(in fact the only index is greater than string length.)
On the other hand, check out charAt()
in the explanation of StringIndexOutOfBoundsException.
public class StringIndexOutOfBoundsException
extends IndexOutOfBoundsException
Thrown by the charAt method in class String and by other String methods to indicate that an index is either negative or greater than or equal to the size of the string.
After i typed the above i checked the charAt() in the api doc.
It says, in case of charAt(int beginIndex) , it throws the StringIndexOutofBoundsException if beginIndex is greater than String length. So, if you change the parameter to charAt(5), it indeed throws it.(I checked out) . The case of the parameter being equal to the string length is really an anomaly.
I have added the relevant portion of the API doc
substring
public String substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Examples:
"unhappy".substring(2) returns "happy"
"Harbison".substring(3) returns "bison"
"emptiness".substring(9) returns "" (an empty string)

Parameters:
beginIndex - the beginning index, inclusive.
Returns:
the specified substring.
Throws:
IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.

------------------
Regards,
Shree
[This message has been edited by shree vijay (edited December 10, 2000).]
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Balaji,
If I am not mistaken you are searching for the last NUL character which marks the boundary of the String. Java strings are not null terminated. So dont waste your time, there aint no '\u000' after the last character. All you get for your pains is an array out of bounds exception.
Cheers
Sahir
 
Balaji Sadasivam
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shree-
I am impressed with your detailed explanation. Thanks a lot.
I didn't look into the APIs. We have to live with the explanations given in the APIs.
Thanks guys for your time.
Balaji
 
reply
    Bookmark Topic Watch Topic
  • New Topic