+2 votes
in JAVA by
Explain Substring in Java or Java Substring

1 Answer

0 votes
by

A part of String is called substring. In other words, substring is a subset of another String. Java String class provides the built-in substring() method that extract a substring from the given string by using the index values passed as an argument. In case of substring() method startIndex is inclusive and endIndex is exclusive.

Suppose the string is "computer", then the substring will be com, compu, ter, etc.

You can get substring from the given String object by one of the two methods:

public String substring(int startIndex):

This method returns new String object containing the substring of the given string from specified startIndex (inclusive). The method throws an IndexOutOfBoundException when the startIndex is larger than the length of String or less than zero.

public String substring(int startIndex, int endIndex):

This method returns new String object containing the substring of the given string from specified startIndex to endIndex. The method throws an IndexOutOfBoundException when the startIndex is less than zero or startIndex is greater than endIndex or endIndex is greater than length of String.

In case of String:

startIndex: inclusive

endIndex: exclusive

Let's understand the startIndex and endIndex by the code given below.

String s="hello";    

System.out.println(s.substring(0,2)); //returns he  as a substring  

In the above substring, 0 points the first letter and 2 points the second letter i.e., e (because end index is exclusive).

Example of Java substring() method

TestSubstring.java

public class TestSubstring{    

 public static void main(String args[]){    

 String s="SachinTendulkar";    

 System.out.println("Original String: " + s);  

 System.out.println("Substring starting from index 6: " +s.substring(6));//Tendulkar    

 System.out.println("Substring starting from index 0 to 6: "+s.substring(0,6)); //Sachin  

 }  

}    

Output:

Original String: SachinTendulkar

Substring starting from index 6: Tendulkar

Substring starting from index 0 to 6: Sachin

...