×

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 throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

LCM Program in Java

LCM Program in Java The LCM program in Java outputs the LCM of the given numbers. In Arithmetic, the lowest common multiple, least common multiple or smallest common multiple of the...

6 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Ad Hoc Problem on Arrays in Java

Ad hoc problems are issues that arise unexpectedly and require immediate attention. These problems can range from small and straightforward issues to more complex and time-consuming problems. Examples of ad...

3 minutes read.

Arrow Operator in Java

The introduction of arrow functions in ES6 gives you a more precise approach to define JavaScript functions. We can write shorter function syntax thanks to them. Your code will be...

4 minutes read.

JDBC vs ODBC

Difference Between JDBC and ODBC ODBC: ODBC (Open Database Connectivity) is the accepted method for accessing databases among organisations and programmers. A database is linked to other programmes, such as word processors, spreadsheets,...

4 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Java Image

For all other classes used for representing graphical images, Java's Image class serves as an abstract superclass. For images in Java, a specific form of object known as a BufferedImage...

4 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Maximizing Profit in Stock Buy Sell in Java

In this tutorial, we will deal with a popular problem, a favourite of interviewers. The problem is named as Maximising profit in stock Buy Sell. we will see certain approaches...

6 minutes read.

Why String in Immutable in Java?

Why String in Immutable in Java Immutable means unchangeable or unmodifiable.  Strings in Java are immutable, it means once a string is created, it cannot be modified or changed. Any change...

2 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Concurrent Modification Exception In Java

When an object is attempted to be updated concurrently when it is not allowed, the ConcurrentModificationException arises. This error typically occurs while using Java Collection classes. When another thread is iterating...

5 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 FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.