java.lang.StringIndexOutOfBoundsException: String index out of range: -1


Very common exception which every java developer might have gone through.

Reason- When try to access the index of the string’s character which is not available.

String sample = “atul”;                         

//Here you will get the String out of range exception. You are trying to get the index of  ‘,’  which is not the character in the string.

sample = sample.substring(0,sample.lastIndexOf(“,”));

Solution: Character must be present in the string before trying to access it.

About these ads

One Response to java.lang.StringIndexOutOfBoundsException: String index out of range: -1

  1. Selena says:

    You truly created a lot of excellent items in ur article,
    “java.lang.StringIndexOutOfBoundsException: String index out of
    range: -1 | Atul Patel”. I am going to be coming back again to
    ur web page soon. Many thanks ,Lloyd

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: