×

Java String subSequence() method

This method returns a new character sequence i.e. subsequence of current sequence

Syntax:

public CharSequence subSequence(int beginIndex, int endIndex)

Parameter:

beginIndex ? begin index, inclusive.

endIndex ? end index, exclusive.

Return: specified subsequnce

Throws:

It throws IndexOutOfBoundsException if begin or end index is negative , if end is greater than length(), or if start is greater than end.

Java String subSequence() Example 1

public class JavaStringSubSequenceEx1
{
      public static void main(String[] args)
      {
            String s1 = "A subSequence Example.";
            CharSequence cs = s1.subSequence(2, 13);
            System.out.println();
            System.out.println(cs);
        System.out.println();
      }
}

Output:

subsequence

Java String subSequence() Example 2

public class JavaStringSubSequenceEx2
{
   public static void main(String args[])
   {
      String Str = new String("Welcome to tutorialandexample.com");
      System.out.print("Return Value :" );
      System.out.println(Str.subSequence(0, 10) );
      System.out.print("Return Value :" );
     System.out.println(Str.subSequence(10, 29) );
   }
}

Output:

Return Value :Welcome to
Return Value : tutorialandexample

Java String subSequence() method Example 3

import java.lang.Math;
public class JavaStringSubSequenceEx3{
    // driver code
    public static void main(String args[])
    {
        String Str = "Welcome to tutorialandexample";
        // throws an error as index is negative
        System.out.print("Returns: ");
        System.out.println(Str.subSequence(-1, 7));
    }
}

Output:

Returns: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
       at java.lang.String.substring(String.java:1960)
       at java.lang.String.subSequence(String.java:2003)
       at JavaStringSubSequenceEx3.main(JavaStringSubSequenceEx3.java:13)

Java String subSequence() Example 4

public class JavaStringSubSequenceEx4
{
   public static void main(String args[]) {
      String Str = new String("Welcome to Tutorialandexample.com");
          System.out.println(Str.subSequence(11, 29) );
   }
}

Output:

Tutorialandexample

Related Topics

Java.net.ConnectionException

java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make...

3 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Java Integer reverseByte() method

The reverseByte() method of Java Integer class returns the value obtained by reversing the order of the bytes in the 2’s complement binary representation. Syntax public static int reverseByte (int i) Parameters The parameter...

1 minute read.

Java Finally Keyword

The final block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java finally block has...

3 minutes read.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

Caesar Cipher Program in Java

It is among the most popular and straightforward encryption methods. With this method, each letter in the text is swapped out for a letter that is a certain number of...

3 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Virtual Function in Java

In the Operated Oriented Programming language, a virtual function or virtual method is a collection of functions that overrides the functionality of a function in an inheriting class with the same...

4 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

Activity selection problem in Java

The activity selection problem is a multiple objective problem hat requires choosing non-conflicting tasks to complete within a specific amount of time from a list of tasks identified by a...

5 minutes read.

Java Lambda foreach

Before understanding lamda foreach, one must know about foreach loop in Java. foreach loop in Java Since J2SE 5.0, there has been a for-each loop in Java, also known as an extended...

4 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Java String toUpperCase() methods

Java String toUpperCase() method is used to convert all the characters of the String into upper case. Syntax public String toUpperCase() public String toUpperCase(Locale locale) Returns It returns upper case String. Java String toUpperCase() Example 1: public...

1 minute read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.