1
public class StringDemo{

   public static void main(String[] args) {

       String str = "this:";
       int a = str.indexOf(":"); // returns 4
       String subStr = str.substring(a+1); // returns "" <empty string>
       String subStr = str.substring(a+2); // throws exception
      int charAt = str.charAt(a+1); // Throws an StringIndexOutOfBoundsExp.
    }
}

can any one explain why is it returning "" and why it throws an exception

3
  • 1
    did you read the javadoc for both of those methods? Commented May 6, 2013 at 12:04
  • 2
    Read Javadocs : docs.oracle.com/javase/6/docs/api/java/lang/… Commented May 6, 2013 at 12:19
  • 1
    i want to know the reason why is it so Commented May 6, 2013 at 12:28

4 Answers 4

3
str.substring(a+1)

returns the string after the given index (a+1), which is the string after the colon which is an empty string.

str.charAt(a+1)

accesses the array value at the position after the colon which doesn't exist.

Sign up to request clarification or add additional context in comments.

Comments

1

str.substring(a+1) returns a substring from str STARTING at a+1 or 5. Your string doesn't have anything at index 5 so it will return an empty string.

2 Comments

ok aggree with your answer but when i try to access a+2 location which is giving me an exception in substring method so string doesnot have anything at the 6th index so why it throws exception
.substring() will return a blank string when you call it with the size of the string, it will simply return a new empty string. Anything greater and it will throw an exception.
0

No it won't throw IndexOutOfBoundException instead it will return empty String. Same is the case when beginIndex and endIndex is equal, in case of second method. It will only throw StringIndexBoundException when beginIndex is negative, larger than endIndex or larger than length of String.

String subStr = str.substring(a+1); // returns "" . // Because there is Nothing at that place

Its clearly there in the API

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

Read more: http://javarevisited.blogspot.com/2011/10/how-substring-in-java-works.html#ixzz2SVu5lyYo

where as CharAt throws Throws: IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string

Comments

0

I would like to answer first why the result is empty string.

Method:substring(int beginIndex)

is like saying

substring(beginIndex,String.length());

the endIndex specified on your argument is 5 and the length is also 5. Thus if you do substring(0,0) or substring(1,1) or substring(5,5), it will always give an empty string because begin and end Index is equal. To put it in simple language, same begin and end index occupies nothing thus result to empty string.

Why the integer 5 is still a valid index? the answer lies on the Java API itself: Throws: IndexOutOfBoundsException - if beginIndex is negative or larger than the length of this String object.

Reference:

Java API
String substring() method example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.