[ Web Proxy ]
URL:
Viewing: https://www.tutorialspoint.com/java/lang/string_subsequence.htm [Back]  [Original]

Java - String subsequence() Method
Tutorials Courses Jobs Login
Home Java/lang Java - String subsequence() Method

Java - String subsequence() Method



The Java String subSequence() method is used to retrieve a new character sequence that is a subsequence of the given sequence.

This method behaves similar to the substring() method. The only difference is that it returns a CharSequence rather than a String. Internally, the subSequence() method calls the substring() method. The returning CharSequence can be explicitly cast into a String object.

The subsequence begins at the provided index with the char value and ends with the char value at (end-1). The returned sequence has a length (in characters) of end-start; if start is equal to end, an empty sequence is returned.

Syntax

Following is the syntax for Java String subSequence() method

public CharSequence subSequence(int beginIndex, int endIndex)

Parameters

  • beginIndex This is the value of begin index, inclusive.

  • endIndex This is the value of the end index, exclusive.

Return Value

This method returns the specified subsequence.

Getting subsequence From a String using Given Index Example

The following example shows the usage of Java String subSequence() method. Here, we have created a string with value 'tutorialspoint' and trying to retrieve the substring at the index range '2-9'

package com.tutorialspoint;

public class StringDemo {       
   public static void main(String[] args) {
      String str = "Tutorials Point!";   
      System.out.println("string = " + str);   
      
      // returns the specified subsequence from index 2 to 9
      System.out.println("string subsequence = " + str.subSequence(2,9));
   }
}

Output

If you compile and run the above program, it will produce the following result

string = Tutorials Point!
string subsequence = torials

Getting subsequence From a String using Negative Index Example

Following is an example of subSequence() method where the index passed is negative. Since this method doesnot accept negative values Exception will be thrown

public class StringDemo {
   public static void main(String args[]) {
      String s = "Python Programming Language";
      System.out.print("The returned string is: ");
      System.out.println(s.subSequence(-4, 2));
   }
}

Exception

If you compile and run the program above, the output will be displayed as follows

The returned string is: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Range [-4, 2) out of bounds for length 27
      at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
      at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
      at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
      at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
      at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
      at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
      at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
      at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4589)
      at java.base/java.lang.String.substring(String.java:2703)
      at java.base/java.lang.String.subSequence(String.java:2741)
      at StringDemo.main(StringDemo.java:8)

Getting subsequence From a String using Out of Range Index Example

In the following example the index is provided out of range or greater than the length of the given string. This results in an exception

public class StringDemo {
   public static void main(String args[]) {
      String s = "Python Programming Language";
      System.out.print("The returned string is: ");
      System.out.println(s.subSequence(15, 60));
   }
}

Exception

On executing the program above, the output is obtained as follows

The returned string is: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Range [15, 60) out of bounds for length 27
      at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
      at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
      at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
      at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
      at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
      at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
      at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
      at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4589)
      at java.base/java.lang.String.substring(String.java:2703)
      at java.base/java.lang.String.subSequence(String.java:2741)
      at StringDemo.main(StringDemo.java:7)

Getting subsequence From a String Containining Meta Characters Example

In the following example we are creating a string containing meta characters. Then we are trying to retrieve the substring with in the range'5-15'.

package com.tutorialspoint;

public class StringDemo {
   public static void main(String args[]) {
      String s = "$&!@$%&**&%$#@!#$(*%$#";
      System.out.print("The returned string is: ");
      System.out.println(s.subSequence(5, 15));
   }
}

Output

The output of the above program is as follows

The returned string is: %&**&%$#@!
java_lang_string.htm
Tutorix - AI Tutor [Tutorix - AI Tutor]
Print Page
Advertisements

Web Proxy Viewer  |  New URL  |  Original Page