Net-informations.com

Java String substring()

Substring is a part of the String. Java String substring() method returns a new string object from a given String.


Substring in Java examples

There are two ways you can use the Java substring() method

  1. String substring(begIndex)
  2. String substring(begIndex, endIndex)

substring(beginIndex)

This method will return a new String object from the specified startIndex (inclusive) and up to the last character of the String.

Syntax

  1. beginIndex – the beginning index, inclusive.

The above code returns String Tutorial . That means the substring function extract the new string from 5th character (start index 5 ).

Full Source

Output

substring(beginIndex,endIndex)

This method creates a new String object containing the contents of the existing String object from specified startIndex up to the specified endIndex . Also in this method, startIndex is inclusive but endIndex is exclusive , which means if you want to remove the last character then you need to give substring(0, string.length()-1).

Syntax

  1. beginIndex – the beginning index, inclusive.
  2. endIndex – the ending index, exclusive.

The above code returns String , that means it extract the string from the 5th character to 11th character.

Full Source

Output

How to extract first 4 character from a string?

Output

How to extract last 8 characters from a String?

Output

How to print one char on each line?

Output

Checking Palindrome using substring() Method


Important points about substring() in Java

In the following Java program , checking if the first letter and the last letter of the string is same or not. If they are not the same, return false . If it is same, call the checkPal method again recursively passing the string again after the first and last letter removed.

java StringIndexOutOfBounds Exception when using String.substring()

When using the Java substring() method, a subset of the character sequence can be extracted from a string. The substring index must be any value from 0 to the length of a string. The java.lang.StringIndexOutOfBoundsException thrown by String methods to indicate that the beginIndex is negative , or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex. The StringIndexOutOfBoundsException is one of the unchecked exceptions in Java.

example

output

Here you can see the length of the string is "3" and we try to extract the 4th character from the string. So, the above code would throw StirngIndexOutOfBoundException as its trying to access index 4 which doesn't exits.


Java Exception handling

How to solve the StringIndexOutOfBoundsException

  1. Check the length of the string.
  2. Exception handling using try...catch.

Check the length of the string

You can check the length of the string using String.length() method before using subString() and proceed to access the characters of it accordingly.

Exception handling using try...catch

Using Java exception handling can be easily solved once you make use of the stack trace provided by the JVM. Once you detect the method that threw the StringIndexOutOfBoundsException , then you must examine the provided arguments are valid or not.










net-informations.com (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About