Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
charactersequence
communitycreator

What is CharSequenceUtils.subSequence in Java?

abhilash

subSequence() is a staticmethods in Java that can be called without creating an object of the class. method of the CharSequenceUtils class. This method returns a subsequence of type CharSequence of the initial character sequence provided to the method, starting from the specified position.

How to import CharSequenceUtils

The definition of CharSequenceUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file.


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the CharSequenceUtils class as follows.

import org.apache.commons.lang3.CharSequenceUtils;

Syntax


public static CharSequence subSequence(final CharSequence cs, final int start)

Parameters

  • cs: The input character sequence.

  • start: The starting index for the subsequence to be retrieved.

Return value

This method returns a new subsequence starting from the given index.

Code

import org.apache.commons.lang3.CharSequenceUtils;

public class main{

    public static void main(String[] args){
        String characterSequence = "hello-educative";
        int startIndex = 5;
        System.out.printf("The output of CharSequenceUtils.subSequence() for the string - '%s' is '%s'", characterSequence, CharSequenceUtils.subSequence(characterSequence, startIndex));
        System.out.println();

        characterSequence = null;
        System.out.printf("The output of CharSequenceUtils.subSequence() for the string - '%s' is '%s'", characterSequence, CharSequenceUtils.subSequence(characterSequence, startIndex));
        System.out.println();

        characterSequence = "hello-educative";
        startIndex = -1;
        System.out.printf("The output of CharSequenceUtils.subSequence() for the string - '%s' is '%s'", characterSequence, CharSequenceUtils.subSequence(characterSequence, startIndex));
        System.out.println();

    }
}

Output


The output of CharSequenceUtils.subSequence() for the string - 'hello-educative' is '-educative'
The output of CharSequenceUtils.subSequence() for the string - 'null' is 'null'
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin -1, end 15, length 15
	at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3319)
	at java.base/java.lang.String.substring(String.java:1874)
	at java.base/java.lang.String.subSequence(String.java:1913)
	at org.apache.commons.lang3.CharSequenceUtils.subSequence(CharSequenceUtils.java:56)
	at Main.main(Main.java:17)

Explanation

Example 1

  • characterSequence = "hello-educative"
  • startIndex = 5

The method returns the substring "-educative" starting at index 5 of the input string.

Example 2

  • characterSequence = null

The method returns null, as the input string is null.

Example 3

  • characterSequence = "hello-educative"
  • startIndex = -1

The method throws an exception because startIndex is out of bounds.

RELATED TAGS

java
charactersequence
communitycreator
RELATED COURSES

View all Courses

Keep Exploring