Java String subSequence()

This article will teach you how to get a subsequence from a String. This looks similar to the Java String substring() method, but the substring method returns a new String while the subSequence method returns CharSequence rather than String.

The subSequence() method is implemented in the String class because Java 1.4 introduced the CharSequence interface, and String implements this interface. This method internally invokes the substring() method.

Post On:String subSequence() Method in Java
Post Type:Java Tutorials
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:SoftwareTestingo Telegram Group

In this blog post, we will learn how to use String subSequence in Java Programming language with an example.

Syntax:

The syntax of the subSequence() Method is:

// It will return Character Sequence 
public CharSequence subSequence(int Startig_index, int End_index); 

//In order to use in program
String_Object.subSequence(int Startig_index, int End_Index)

Here the subSequence() method takes two input parameters which should be an integer value. The First parameter informs where the extraction will start, and the second parameter informs where the extraction will end.

And the String.subSequence method will return the character sequence starting from the Starting_index up to End_index but not included.

Tips: When we are invoking the subSequence() method, it internally invokes the substring() method. And the code snippet looks something like the below:

public CharSequence subSequence(int beginIndex, int endIndex) 
{
    return this.substring(beginIndex, endIndex);
}

Program:

Example Of Java String subSequence(): The code snippet below demonstrates how to use the subSequence() method. This method returns CharSequence from a string.

Program: Write a Java program to retrieve Substring using subSequence().
package com.SoftwareTestingo.JavaBasics;
public class SubSequenceExample 
{
	public static void main(String[] args) 
	{
		String str = "www.SoftwareTestingo.com";
		System.out.println("Last 4 char String: " + str.subSequence(str.length() - 4, str.length()));
		System.out.println("First 4 char String: " + str.subSequence(0, 4));
		System.out.println("website name: " + str.subSequence(4, 20));
		// substring vs subSequence
		//When you are compare with == its comparing Hashcode which is different 
		System.out.println("substring == subSequence ? " + (str.substring(4, 14) == str.subSequence(4, 14)));
		//When you compare with equals(), its comparing value
		System.out.println("substring equals subSequence ? " + (str.substring(4, 14).equals(str.subSequence(4, 14))));
	}
}

Output:

Last 4 char String: .com
First 4 char String: www.
website name: SoftwareTestingo
substring == subSequence ? false
substring equals subSequence ? true

String subSequence() With Negative Example: In the below example, we will try to execute by entering the negative value in the index position.

If you pass a negative value in the index, you will get StringIndexOutOfBoundsException.

package com.SoftwareTestingO.Java.basics;
public class SubSequenceExample2 
{
	public static void main(String[] args) 
	{
		String stringObject = "This example is with negative index";

		CharSequence output = stringObject.subSequence(-10, stringObject.length());
		System.out.println(output);
	}
}

Output:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin -10, end 35, length 35
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4604)
	at java.base/java.lang.String.substring(String.java:2707)
	at java.base/java.lang.String.subSequence(String.java:2745)
	at com.softwaretestingo.string.SubSequenceExample2.main(SubSequenceExample2.java:8)

Final Words:


We hope that you now have a clear understanding of the use of subSequence() in Java. If you still have any questions or need further clarification, we encourage you to ask us in the comment section below. Our team is here to assist you and provide any additional information you may require. Your feedback and engagement are valuable to us, so please don’t hesitate to reach out.

What will happen if the index is negative on subSequence()?

Java does not support negative indexes for subSequence() like Python, which will result in a runtime exception IndexOutOfBoundsException.

What is the difference between substring() and subSequence()?

The substring() method returns a string, while the subSequence() method returns a CharSequence. The main difference is that in the subSequence() method, you cannot pass a single start index like you can with the substring() method. SubSequence ()’s internal implementation is based on the substring() method.

Avatar for Softwaretestingo Editorial Board

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment