Java: String substring() method

In this article, we will discuss about how to get substring (partial string) using String’s substring() method

 

substring() method:

  • This String method returns substring for the specified begin value and end value

Note:

  • there are 2 variants or overloaded substring() methods
  • 1st variant returns substring starting from specified index-position till length
  • Whereas 2nd variant returns substring starting from specified index-position to specified end index-position
  • In addition to this, there is one String method similar to 2nd variant i.e.; subSequence() method

Method Signature:

public String substring(int beginIndex);

public String substring(int beginIndex, int endIndex);

public CharSequence subSequence(int beginIndex, int endIndex);

Parameters:

  • beginIndex  –> start index (from where string need to be extracted)
  • endIndex     –> end index (till where string need to be extracted)

Returns:

substring() method Returns
public String substring(int beginIndex); Returns substring from the invoking string, starting from specified begin index-position
public String substring(int beginIndex, int endIndex); Returns substring from the invoking string, starting from specified begin index-position till end index-position
public CharSequence subSequence(int beginIndex, int endIndex); very similar to substring(beginIndex, endIndex);

Returns char sequence from the invoking string, starting from specified begin index-position till end index-position

Throws:

  • IndexOutOfBoundsException, if index value supplied/passed falls out of range
  • IndexOutOfBoundsException is thrown, if the input index value is out of range i.e.;
    1. Index-position for begin-index is negative (<0)
    2. Index-position for begin-index is greater than length()-1
    3. Index-position for end-index is greater than length of the String

 

Examples on substring() method:

Example 1: Java program to get substring starting from specified begin index-position

Method Signature:

public String substring(int beginIndex);

StringSubStringMethod.java

package in.bench.resources.string.methods;

public class StringSubStringMethod {

	public static void main(String[] args) {

		// Example 1: to get substring() - 
		// starting from 10th index-position
		String testStr1 = "Java is a super cool language";
		String subString1 = testStr1.substring(10);
		System.out.println("1st substring(10) is : " 
				+ subString1);

		// Example 2: to get substring() - 
		// starting from 24th index-position
		String testStr2 = "BenchResources.Net is a Java weblog";
		String subString2 = testStr2.substring(24);
		System.out.println("\n2nd substring(24) is : " 
				+ subString2);

		// Example 3: to get substring() - 
		// starting from 13th index-position
		String testStr3 = "String class has useful methods";
		String subString3 = testStr3.substring(13);
		System.out.println("\n3rd substring(13) is : " 
				+ subString3);
	}
}

Output:

1st substring(10) is : super cool language

2nd substring(24) is : Java weblog

3rd substring(13) is : has useful methods

 

Example 2: Java program to get substring starting from specified begin index-position till specified end index-position

Method Signature:

public String substring(int beginIndex, int endIndex);

StringSubStringMethod2.java

package in.bench.resources.string.methods;

public class StringSubStringMethod2 {

	public static void main(String[] args) {

		// Example 1: to get substring() - 
		// starting from 10th till 20th index-position
		String testStr1 = "Java is a super cool language";
		String subString1 = testStr1.substring(10, 20);
		System.out.println("1st substring(10, 20) is : " 
				+ subString1);

		// Example 2: to get substring() - 
		// starting from 24th till 28th index-position
		String testStr2 = "BenchResources.Net is a Java weblog";
		String subString2 = testStr2.substring(24, 28);
		System.out.println("\n2nd substring(24, 28) is : " 
				+ subString2);

		// Example 3: to get substring() - 
		// starting from 7th till 23rd index-position
		String testStr3 = "String class has useful methods";
		String subString3 = testStr3.substring(7, 23);
		System.out.println("\n3rd substring(7, 23) is : " 
				+ subString3);
	}
}

Output:

1st substring(10, 20) is : super cool

2nd substring(24, 28) is : Java

3rd substring(7, 23) is : class has useful

 

Example 3: Java program to get char sequence starting from specified begin index-position till specified end index-position

  • Note: very similar to substring(beginIndex, endIndex);

Method Signature:

public CharSequence subSequence(int beginIndex, int endIndex);

StringSubSequenceMethod.java

package in.bench.resources.string.methods;

public class StringSubSequenceMethod {

	public static void main(String[] args) {

		// Example 1: to get subSequence() - 
		// starting from 10th till 20th index-position
		String testStr1 = "Java is a super cool language";
		CharSequence charSequence1 = testStr1.subSequence(10, 20);
		System.out.println("1st subSequence(10, 20) is : " 
				+ charSequence1);

		// Example 2: to get subSequence() - 
		// starting from 24th till 28th index-position
		String testStr2 = "BenchResources.Net is a Java weblog";
		CharSequence charSequence2 = testStr2.subSequence(24, 28);
		System.out.println("\n2nd subSequence(24, 28) is : " 
				+ charSequence2);

		// Example 3: to get subSequence() - 
		// starting from 7th till 23rd index-position
		String testStr3 = "String class has useful methods";
		CharSequence charSequence3 = testStr3.subSequence(7, 23);
		System.out.println("\n3rd subSequence(7, 23) is : " 
				+ charSequence3);
	}
}

Output:

1st subSequence(10, 20) is : super cool

2nd subSequence(24, 28) is : Java

3rd subSequence(7, 23) is : class has useful

 

Hope, you found this article very helpful. If you have any suggestions or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

 

References:

 

Happy Coding !!
Happy Learning !!

Java: String toCharArray() method
Java: String startsWith() method