Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the string subSequence() method in Java?

Kainat Asif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The subSequence() method in Java is used to return a subsequence of a sequence.

Syntax

The subSequence() method can be declared as shown in the code snippet below:

public CharSequence subSequence(int start, int end) 
  • start (inclusive): The index from which the subsequence will start.
  • end (exclusive): The index at which the subsequence will end.

Return value

The subSequence() method returns the subsequence of the sequence.

Note: The subSequence() method throws the IndexOutOfBoundsException if any of the following conditions are met:

  • start is less than 0.
  • end is less than 0.
  • start is greater than end.
  • end is greater than the length of the string.

Example

Consider the code snippet below, which demonstrates the use of the subSequence() method.

import java.lang.Math;
class Main {
public static void main(String args[])
{
String str = "subSequence() example";
System.out.println("str: " + str);
System.out.println("subStr: " + str.subSequence(0, 13));
}
}

Explanation

A string str is declared in line 8. The subSequence() method is used in line 11 to get the subsequence of str.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring